home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / drivers / scsi / atari_NCR5380.c next >
Encoding:
C/C++ Source or Header  |  1994-06-11  |  84.1 KB  |  2,791 lines

  1. /* 
  2.  * NCR 5380 generic driver routines.  These should make it *trivial*
  3.  *     to implement 5380 SCSI drivers under Linux with a non-trantor
  4.  *    architecture.
  5.  *
  6.  *    Note that these routines also work with NR53c400 family chips.
  7.  *
  8.  * Copyright 1993, Drew Eckhardt
  9.  *    Visionary Computing 
  10.  *    (Unix and Linux consulting and custom programming)
  11.  *     drew@colorado.edu
  12.  *    +1 (303) 666-5836
  13.  *
  14.  * DISTRIBUTION REALEASE 4. 
  15.  *
  16.  * For more information, please consult 
  17.  *
  18.  * NCR 5380 Family
  19.  * SCSI Protocol Controller
  20.  * Databook
  21.  *
  22.  * NCR Microelectronics
  23.  * 1635 Aeroplaza Drive
  24.  * Colorado Springs, CO 80916
  25.  * 1+ (719) 578-3400
  26.  * 1+ (800) 334-5454
  27.  */
  28.  
  29. /*
  30.  * ++roman: To port the 5380 driver to the Atari, I had to do some changes in
  31.  * this file, too:
  32.  *
  33.  *  - Some of the debug statements were incorrect (undefined variables and the
  34.  *    like). I fixed that.
  35.  *
  36.  *  - In information_transfer(), I think a #ifdef was wrong. Looking at the
  37.  *    possible DMA transfer size should also happen for REAL_DMA. I added this
  38.  *    in the #if statement.
  39.  *
  40.  *  - When using real DMA, information_transfer() should return in a DATAOUT
  41.  *    phase after starting the DMA. It has nothing more to do.
  42.  *
  43.  *  - The interrupt service routine should run main after end of DMA, too (not
  44.  *    only after RESELECTION interrupts). Additionally, it should _not_ test
  45.  *    for more interrupts after running main, since a DMA process may have
  46.  *    been started and interrupts are turned on now. The new int could happen
  47.  *    inside the execution of NCR5380_intr(), leading to recursive
  48.  *    calls.
  49.  *
  50.  *  - I've added a function merge_consecutive_buffers() that trys to
  51.  *    merge scatter-gather buffers that are located at consecutive
  52.  *    physical addresses and can be processed with the same DMA setup.
  53.  *    Since most scatter-gather operations work on a page (4K) of
  54.  *    4 buffers (1K), in more than 90% of all cases three interrupts and
  55.  *    DMA setup actions are saved.
  56.  * 
  57.  */
  58.  
  59.  
  60.  
  61. /*
  62.  * $Log: NCR5380.c,v $
  63.  * Revision 1.5  1994/01/19  09:14:57  drew
  64.  * Fixed udelay() hack that was being used on DATAOUT phases
  65.  * instead of a propper wait for the final handshake.
  66.  *
  67.  * Revision 1.4  1994/01/19  06:44:25  drew
  68.  * *** empty log message ***
  69.  *
  70.  * Revision 1.3  1994/01/19  05:24:40  drew
  71.  * Added support for TCR LAST_BYTE_SENT bit.
  72.  *
  73.  * Revision 1.2  1994/01/15  06:14:11  drew
  74.  * REAL DMA support, bug fixes.
  75.  *
  76.  * Revision 1.1  1994/01/15  06:00:54  drew
  77.  * Initial revision
  78.  *
  79.  */
  80.  
  81. /*
  82.  * Furthur development / testing that should be done : 
  83.  * 1.  Cleanup the NCR5380_transfer_dma function and DMA operation complete
  84.  *     code so that everything does the same thing that's done at the 
  85.  *     end of a pseudo-DMA read operation.
  86.  *
  87.  * 2.  Fix REAL_DMA (interrupt driven, polled works fine) -
  88.  *     basically, transfer size needs to be reduced by one 
  89.  *     and the last byte read as is done with PSEUDO_DMA.
  90.  * 
  91.  * 3.  Test USLEEP code 
  92.  *
  93.  * 4.  Test SCSI-II tagged queueing (I have no devices which support 
  94.  *    tagged queueing)
  95.  *
  96.  * 5.  Test linked command handling code after Eric is ready with 
  97.  *      the high level code.
  98.  */
  99.  
  100. #ifndef notyet
  101. #undef LINKED
  102. #undef USLEEP
  103. /* #undef REAL_DMA */
  104. #endif
  105.  
  106. #ifdef REAL_DMA_POLL
  107. #undef READ_OVERRUNS
  108. #define READ_OVERRUNS
  109. #endif
  110.  
  111. /*
  112.  * Design
  113.  * Issues :
  114.  *
  115.  * The other Linux SCSI drivers were written when Linux was Intel PC-only,
  116.  * and specifically for each board rather than each chip.  This makes their
  117.  * adaptation to platforms like the Mac (Some of which use NCR5380's)
  118.  * more difficult than it has to be.
  119.  *
  120.  * Also, many of the SCSI drivers were written before the command queing
  121.  * routines were implemented, meaning their implementations of queued 
  122.  * commands were hacked on rather than designed in from the start.
  123.  *
  124.  * When I designed the Linux SCSI drivers I figured that 
  125.  * while having two different SCSI boards in a system might be useful
  126.  * for debugging things, two of the same type wouldn't be used.
  127.  * Well, I was wrong and a number of users have mailed me about running
  128.  * multiple high-performance SCSI boards in a server.
  129.  *
  130.  * Finally, when I get questions from users, I have no idea what 
  131.  * revision of my driver they are running.
  132.  *
  133.  * This driver attempts to address these problems :
  134.  * This is a generic 5380 driver.  To use it on a different platform, 
  135.  * one simply writes appropriate system specific macros (ie, data
  136.  * transfer - some PC's will use the I/O bus, 68K's must use 
  137.  * memory mapped) and drops this file in their 'C' wrapper.
  138.  *
  139.  * As far as command queueing, two queues are maintained for 
  140.  * each 5380 in the system - commands that haven't been issued yet,
  141.  * and commands that are currently executing.  This means that an 
  142.  * unlimited number of commands may be queued, letting 
  143.  * more commands propogate from the higher driver levels giving higher 
  144.  * througput.  Note that both I_T_L and I_T_L_Q nexuses are supported, 
  145.  * allowing multiple commands to propogate all the way to a SCSI-II device 
  146.  * while a command is allready executing.
  147.  *
  148.  * To solve the multiple-boards-in-the-same-system problem, 
  149.  * there is a separate instance structure for each instance
  150.  * of a 5380 in the system.  So, mutliple NCR5380 drivers will
  151.  * be able to coexist with appropriate changes to the high level
  152.  * SCSI code.  
  153.  *
  154.  * A NCR5380_PUBLIC_REVISION macro is provided, with the release
  155.  * number (updated for each public release) printed by the 
  156.  * NCR5380_print_options command, which should be called from the 
  157.  * wrapper detect function, so that I know what release of the driver
  158.  * users are using.
  159.  *
  160.  * Issues specific to the NCR5380 : 
  161.  *
  162.  * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead 
  163.  * piece of hardware that requires you to sit in a loop polling for 
  164.  * the REQ signal as long as you are connected.  Some devices are 
  165.  * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect 
  166.  * while doing long seek operations.
  167.  * 
  168.  * The workarround for this is to keep track of devices that have
  169.  * disconnected.  If the device hasn't disconnected, for commands that
  170.  * should disconnect, we do something like 
  171.  *
  172.  * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
  173.  * 
  174.  * Some tweaking of N and M needs to be done.  An algorithm based 
  175.  * on "time to data" would give the best results as long as short time
  176.  * to datas (ie, on the same track) were considered, however these 
  177.  * broken devices are the exception rather than the rule and I'd rather
  178.  * spend my time optomizing for the normal case.
  179.  *
  180.  * Architecture :
  181.  *
  182.  * At the heart of the design is a corroutine, NCR5380_main,
  183.  * which is started when not running by the interrupt handler,
  184.  * timer, and queue command function.  It attempts to establish
  185.  * I_T_L or I_T_L_Q nexuses by removing the commands from the 
  186.  * issue queue and calling NCR5380_select() if a nexus 
  187.  * is not established. 
  188.  *
  189.  * Once a nexus is established, the NCR5380_information_transfer()
  190.  * phase goes through the various phases as instructed by the target.
  191.  * if the target goes into MSG IN and sends a DISCONNECT message,
  192.  * the command structure is placed into the per instance disconnected
  193.  * queue, and NCR5380_main tries to find more work.  If USLEEP
  194.  * was defined, and the target is idle for too long, the system
  195.  * will try to sleep.
  196.  *
  197.  * If a command has disconnected, eventually an interrupt will trigger,
  198.  * calling NCR5380_intr()  which will inturn call NCR5380_reselect
  199.  * to restablish a nexus.  This will run main if necessary.
  200.  *
  201.  * On command termination, the done function will be called as 
  202.  * appropriate.
  203.  *
  204.  * SCSI pointers are maintained in the SCp field of SCSI command 
  205.  * structures, being initialized after the command is connected
  206.  * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
  207.  * Note that in violation of the standard, an implicit SAVE POINTERS operation
  208.  * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
  209.  */
  210.  
  211. /*
  212.  * Using this file :
  213.  * This file a skeleton Linux SCSI driver for the NCR 5380 series
  214.  * of chips.  To use it, you write a architecture specific functions 
  215.  * and macros and include this file in your driver.
  216.  *
  217.  * These macros control options : 
  218.  * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be 
  219.  *    defined.
  220.  * 
  221.  * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
  222.  *    for commands that return with a CHECK CONDITION status. 
  223.  *
  224.  * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
  225.  *     tracievers. 
  226.  *
  227.  * LINKED - if defined, linked commands are supported.
  228.  *
  229.  * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
  230.  *
  231.  * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
  232.  *
  233.  * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
  234.  *     rely on phase mismatch and EOP interrupts to determine end 
  235.  *    of phase.
  236.  *
  237.  * SCSI2 - if defined, SCSI-2 tagged queing is used where possible
  238.  *
  239.  * UNSAFE - leave interrupts enabled during pseudo-DMA transfers.  You
  240.  *        only really want to use this if you're having a problem with
  241.  *        dropped characters during high speed communications, and even
  242.  *        then, you're going to be better off twiddling with transfersize
  243.  *        in the high level code.
  244.  *
  245.  * USLEEP - if defined, on devices that aren't disconnecting from the 
  246.  *    bus, we will go to sleep so that the CPU can get real work done 
  247.  *    when we run a command that won't complete immediately.
  248.  *
  249.  * Note that if USLEEP is defined, NCR5380_TIMER *must* also be
  250.  * defined.
  251.  *
  252.  * Defaults for these will be provided if USLEEP is defined, although
  253.  * the user may want to adjust these to allocate CPU resources to 
  254.  * the SCSI driver or "real" code.
  255.  * 
  256.  * USLEEP_SLEEP - amount of time, in jiffies, to sleep
  257.  *
  258.  * USLEEP_POLL - amount of time, in jiffies, to poll
  259.  *
  260.  * These macros MUST be defined :
  261.  * NCR5380_local_declare() - declare any local variables needed for your transfer
  262.  *    routines.
  263.  *
  264.  * NCR5380_setup(instance) - initialize any local variables needed from a given
  265.  *    instance of the host adapter for NCR5380_{read,write,pread,pwrite}
  266.  * 
  267.  * NCR5380_read(register)  - read from the specified register
  268.  *
  269.  * NCR5380_write(register, value) - write to the specific register 
  270.  *
  271.  * NCR5380_implementation_fields  - additional fields needed for this 
  272.  *    specific implementation of the NCR5380
  273.  *
  274.  * Either real DMA *or* pseudo DMA may be implemented
  275.  * REAL functions : 
  276.  * NCR5380_REAL_DMA should be defined if real DMA is to be used.
  277.  * Note that the DMA setup functions should return the number of bytes 
  278.  *    that they were able to program the controller for.
  279.  *
  280.  * Also note that generic i386/PC versions of these macros are 
  281.  *    available as NCR5380_i386_dma_write_setup,
  282.  *    NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
  283.  *
  284.  * NCR5380_dma_write_setup(instance, src, count) - initialize
  285.  * NCR5380_dma_read_setup(instance, dst, count) - initialize
  286.  * NCR5380_dma_residual(instance); - residual count
  287.  *
  288.  * PSEUDO functions :
  289.  * NCR5380_pwrite(instance, src, count)
  290.  * NCR5380_pread(instance, dst, count);
  291.  *
  292.  * If nothing specific to this implementation needs doing (ie, with external
  293.  * hardware), you must also define 
  294.  *  
  295.  * NCR5380_queue_command
  296.  * NCR5380_reset
  297.  * NCR5380_abort
  298.  *
  299.  * to be the global entry points into the specific driver, ie 
  300.  * #define NCR5380_queue_command t128_queue_command.
  301.  *
  302.  * If this is not done, the routines will be defined as static functions
  303.  * with the NCR5380* names and the user must provide a globally
  304.  * accessable wrapper function.
  305.  *
  306.  * The generic driver is initialized by calling NCR5380_init(instance),
  307.  * after setting the appropriate host specific fields and ID.  If the 
  308.  * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
  309.  * possible) function may be used.  Before the specific driver initialization
  310.  * code finishes, NCR5380_print_options should be called.
  311.  */
  312.  
  313. static struct Scsi_Host *first_instance = NULL;
  314. static Scsi_Host_Template *the_template = NULL;
  315.  
  316. /*
  317.  * Function: void merge_consecutive_buffers( Scsi_Cmnd *cmd )
  318.  *
  319.  * Purpose: Try to merge several scatter-gather requests into one DMA
  320.  *    transfer. This is possible if the scatter buffers lie on
  321.  *    physical consecutive addresses.
  322.  *
  323.  * Parameters: Scsi_Cmnd *cmd
  324.  *    The command to work on. The first scatter buffer's data are
  325.  *    assumed to be already transfered into ptr/this_residual.
  326.  */
  327.  
  328. /* A special issue is when the buffer is exactly at the end of the
  329.  * last physical memory chunk: VTOP would have to calculate the
  330.  * physical address just 1 byte behind the end of physical memory. But
  331.  * it will panic if given this address :-( So we need to avoid calling
  332.  * VTOP on addresses that don't exist. This is done by keeping
  333.  * 'endadr' to be the real end address of the buffer, not one byte
  334.  * more (which would be easier).
  335.  */
  336.  
  337. static void merge_consecutive_buffers( Scsi_Cmnd *cmd )
  338.  
  339. {
  340.     unsigned long endadr;
  341. #if (NDEBUG & NDEBUG_MERGING)
  342.     unsigned long oldlen = cmd->SCp.this_residual;
  343.     int          cnt = 1;
  344. #endif
  345.  
  346.     for( endadr = VTOP(cmd->SCp.ptr + cmd->SCp.this_residual - 1);
  347.      cmd->SCp.buffers_residual &&
  348.      /* Note that, if READ_OVERRUNS is defined, the virtual
  349.       * addresses have to be consecutive, too. The last two bytes
  350.       * are done by PIO and that uses virtual addresses.
  351.       */
  352. #ifdef READ_OVERRUNS
  353.      (cmd->SCp.buffer+1)->address ==
  354.          cmd->SCp.ptr + cmd->SCp.this_residual &&
  355. #endif
  356.      VTOP( (cmd->SCp.buffer+1)->address ) == endadr + 1; ) {
  357.     
  358. #if (NDEBUG & NDEBUG_MERGING)
  359.     printk( "%08lx == %08lx -> merging\n",
  360.         VTOP( (cmd->SCp.buffer+1)->address ), endadr );
  361.     ++cnt;
  362. #endif
  363.     ++cmd->SCp.buffer;
  364.     --cmd->SCp.buffers_residual;
  365.     cmd->SCp.this_residual += cmd->SCp.buffer->length;
  366.     endadr += cmd->SCp.buffer->length;
  367.     }
  368. #if (NDEBUG & NDEBUG_MERGING)
  369.     if (oldlen != cmd->SCp.this_residual)
  370.     printk( "merged %d buffers from %08lx, new length %08lx\n",
  371.         cnt, (long)(cmd->SCp.ptr), cmd->SCp.this_residual );
  372. #endif
  373. }
  374.  
  375. /*
  376.  * Function : void initialize_SCp(Scsi_Cmnd *cmd)
  377.  *
  378.  * Purpose : initialize the saved data pointers for cmd to point to the 
  379.  *    start of the buffer.
  380.  *
  381.  * Inputs : cmd - Scsi_Cmnd structure to have pointers reset.
  382.  */
  383.  
  384. static __inline__ void initialize_SCp(Scsi_Cmnd *cmd) {
  385.     /* 
  386.      * Initialize the Scsi Pointer field so that all of the commands in the 
  387.      * various queues are valid.
  388.      */
  389.  
  390.     if (cmd->use_sg) {
  391.     cmd->SCp.buffer = (struct scatterlist *) cmd->buffer;
  392.     cmd->SCp.buffers_residual = cmd->use_sg - 1;
  393.     cmd->SCp.ptr = (char *) cmd->SCp.buffer->address;
  394.     cmd->SCp.this_residual = cmd->SCp.buffer->length;
  395.     /* ++roman: Try to merge some scatter-buffers if they are at
  396.      * consecutive physical addresses.
  397.      */
  398.     merge_consecutive_buffers( cmd );
  399.     } else {
  400.     cmd->SCp.buffer = NULL;
  401.     cmd->SCp.buffers_residual = 0;
  402.     cmd->SCp.ptr = (char *) cmd->request_buffer;
  403.     cmd->SCp.this_residual = cmd->request_bufflen;
  404.     }
  405. }
  406.  
  407. #include <linux/delay.h>
  408.  
  409. #ifdef NDEBUG
  410. static struct {
  411.     unsigned char mask;
  412.     char * name;} 
  413. signals[] = {{ SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" }, 
  414.     { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD,  "CD" }, { SR_IO, "IO" }, 
  415.     { SR_SEL, "SEL" }, {0, NULL}}, 
  416. basrs[] = {{BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}},
  417. icrs[] = {{ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
  418.     {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"}, 
  419.     {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"}, 
  420.     {0, NULL}},
  421. mrs[] = {{MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"}, 
  422.     {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR, 
  423.     "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
  424.     {MR_MONITOR_BSY, "MODE MONITOR BSY"},
  425.     {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"}, 
  426.     {0, NULL}};
  427.  
  428. /*
  429.  * Function : void NCR5380_print(struct Scsi_Host *instance)
  430.  *
  431.  * Purpose : print the SCSI bus signals for debugging purposes
  432.  *
  433.  * Input : instance - which NCR5380
  434.  */
  435.  
  436. static void NCR5380_print(struct Scsi_Host *instance) {
  437.     NCR5380_local_declare();
  438.     unsigned char status, data, basr, mr, icr, i;
  439.     NCR5380_setup(instance);
  440.     cli();
  441.     data = NCR5380_read(CURRENT_SCSI_DATA_REG);
  442.     status = NCR5380_read(STATUS_REG);
  443.     mr = NCR5380_read(MODE_REG);
  444.     icr = NCR5380_read(INITIATOR_COMMAND_REG);
  445.     basr = NCR5380_read(BUS_AND_STATUS_REG);
  446.     sti();
  447.     for (i = 0; signals[i].mask ; ++i) 
  448.     if (status & signals[i].mask)
  449.         printk(" %s", signals[i].name);
  450.     for (i = 0; basrs[i].mask ; ++i) 
  451.     if (basr & basrs[i].mask)
  452.         printk(" %s", basrs[i].name);
  453.     for (i = 0; icrs[i].mask; ++i) 
  454.     if (icr & icrs[i].mask)
  455.         printk(" %s", icrs[i].name);
  456.     for (i = 0; mrs[i].mask; ++i) 
  457.     if (mr & mrs[i].mask)
  458.         printk(" %s", mrs[i].name);
  459.     printk("\n");
  460. }
  461.  
  462. static struct {
  463.     unsigned char value;
  464.     char *name;
  465. } phases[] = {
  466.     {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
  467.     {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
  468.     {PHASE_UNKNOWN, "UNKNOWN"}};
  469.  
  470. /* 
  471.  * Function : void NCR5380_print_phase(struct Scsi_Host *instance)
  472.  *
  473.  * Purpose : print the current SCSI phase for debugging purposes
  474.  *
  475.  * Input : instance - which NCR5380
  476.  */
  477.  
  478. static void NCR5380_print_phase(struct Scsi_Host *instance)
  479.  
  480. {
  481.     NCR5380_local_declare();
  482.     unsigned char status;
  483.     int i;
  484.     NCR5380_setup(instance);
  485.  
  486.     status = NCR5380_read(STATUS_REG);
  487.     if (!(status & SR_REQ)) 
  488.     printk("scsi%d : REQ not asserted, phase unknown.\n", 
  489.         instance->host_no);
  490.     else {
  491.     for (i = 0; (phases[i].value != PHASE_UNKNOWN) && 
  492.         (phases[i].value != (status & PHASE_MASK)); ++i); 
  493.     printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
  494.     }
  495. }
  496. #endif
  497.  
  498. /*
  499.  * We need to have our corroutine active given these constraints : 
  500.  * 1.  The mutex flag, main_running, can only be set when the main 
  501.  *     routine can actually process data, otherwise SCSI commands
  502.  *     will never get issued.
  503.  *
  504.  * 2.  NCR5380_main() shouldn't be called before it has exited, because
  505.  *     other drivers have had kernel stack overflows in similar
  506.  *     situations.
  507.  *
  508.  * 3.  We don't want to inline NCR5380_main() because of space concerns,
  509.  *     even though it is only called in two places.
  510.  *
  511.  * So, the solution is to set the mutex in an inline wrapper for the 
  512.  * main corroutine, and have the main corroutine exit with interrupts 
  513.  * disabled after the final search through the queues so that no race 
  514.  * conditions are possible.
  515.  */
  516.  
  517. static volatile int main_running = 0;
  518.  
  519. /* 
  520.  * Function : run_main(void)
  521.  * 
  522.  * Purpose : insure that the coroutine is running and will process our 
  523.  *     request.  main_running is checked/set here (in an inline function)
  524.  *    rather than in NCR5380_main itself to reduce the chances of stack
  525.  *    overflow.
  526.  *
  527.  */
  528.  
  529. static __inline__ void run_main(void)
  530.  
  531. {
  532. /*    unsigned long flags; */
  533.  
  534. /*    save_flags(flags); */
  535.     cli();
  536.     if (!main_running) {
  537.     main_running = 1;
  538.         NCR5380_main();
  539.     /* 
  540.          * main_running is cleared in NCR5380_main once it can't do 
  541.      * more work, and NCR5380_main exits with interrupts disabled.
  542.      */
  543.     /* restore_flags(flags); */
  544.     sti();
  545.     } else {
  546.     /* restore_flags(flags); */
  547.     sti();
  548.     }
  549. }
  550.  
  551. #ifdef USLEEP
  552. #ifndef NCR5380_TIMER
  553. #error "NCR5380_TIMER must be defined so that this type of NCR5380 driver gets a unique timer."
  554. #endif
  555.  
  556. /*
  557.  * These need tweaking, and would probably work best as per-device 
  558.  * flags initialized differently for disk, tape, cd, etc devices.
  559.  * People with broken devices are free to experiment as to what gives
  560.  * the best results for them.
  561.  *
  562.  * USLEEP_SLEEP should be a minimum seek time.
  563.  *
  564.  * USLEEP_POLL should be a maximum rotational latency.
  565.  */
  566. #ifndef USLEEP_SLEEP
  567. /* 20 ms (reasonable hard disk speed) */
  568. #define USLEEP_SLEEP 2
  569. #endif
  570. /* 300 RPM (floppy speed) */
  571. #ifndef USLEEP_POLL
  572. #define USLEEP_POLL 20
  573. #endif
  574.  
  575. static struct Scsi_Host * expires_first = NULL;
  576.  
  577. /* 
  578.  * Function : int should_disconnect (unsigned char cmd)
  579.  *
  580.  * Purpose : decide weather a commmand would normally disconnect or 
  581.  *    not, since if it won't disconnect we should go to sleep.
  582.  *
  583.  * Input : cmd - opcode of SCSI command
  584.  *
  585.  * Returns : DISCONNECT_LONG if we should disconnect for a really long 
  586.  *     time (ie always, sleep, look for REQ active, sleep), 
  587.  *    DISCONNECT_TIME_TO_DATA if we would only disconnect for a normal
  588.  *     time-to-data dealy, DISCONNECT_NONE if this command would return
  589.  *     immediately.
  590.  *
  591.  *      Future sleep algorithms based on time to data can exploit 
  592.  *      something like this so they can differentiate between "normal" 
  593.  *    (ie, read, write, seek) and unusual commands (ie, * format).
  594.  *
  595.  * Note : We don't deal with commands that handle an immediate disconnect,
  596.  *        
  597.  */
  598.  
  599. static int should_disconnect (unsigned char cmd)
  600.  
  601. {
  602.     switch (cmd) {
  603.     case READ_6:
  604.     case WRITE_6:
  605.     case SEEK_6:
  606.     case READ_10:
  607.     case WRITE_10:
  608.     case SEEK_10:
  609.     return DISCONNECT_TIME_TO_DATA;
  610.     case FORMAT_UNIT:
  611.     case SEARCH_HIGH:
  612.     case SEARCH_LOW:
  613.     case SEARCH_EQUAL:
  614.     return DISCONNECT_LONG;
  615.     default:
  616.     return DISCONNECT_NONE;
  617.     }
  618. }
  619.  
  620. /*
  621.  * Assumes instance->time_expires has been set in higher level code.
  622.  */
  623.  
  624. static int NCR5380_set_timer (struct Scsi_Host *instance)
  625.  
  626. {
  627.     struct Scsi_Host *tmp, **prev;
  628.     
  629.     cli();
  630.     if (((struct NCR5380_hostdata *) (instance->host_data))->next_timer) {
  631.     sti();
  632.     return -1;
  633.     }
  634.  
  635.     for (prev = &expires_first, tmp = expires_first; tmp; 
  636.     prev = &(((struct NCR5380_hostdata *) tmp->host_data)->next_timer), 
  637.     tmp = ((struct NCR5380_hostdata *) tmp->host_data)->next_timer)
  638.     if (instance->time_expires < tmp->time_expires) 
  639.         break;
  640.        
  641.     instance->next_timer = tmp;
  642.     *prev = instance;
  643.     timer_table[NCR5380_TIMER].expires = expires_first->time_expires;
  644.     timer_active |= 1 << NCR5380_TIMER;
  645.     sti;
  646.     return 0;
  647. }    
  648.  
  649. /* Doing something about unwanted rentrancy here might be useful */
  650. void NCR5380_timer_fn(void)
  651.  
  652. {
  653.     struct Scsi_Host *instance;
  654.     cli();
  655.     for (; expires_first && expires_first->time_expires >= jiffies; ) {
  656.     instance = ((NCR5380_hostdata *) expires_first->host_data)->
  657.         expires_next;
  658.     ((NCR5380_hostdata *) expires_first->host_data)->expires_next = 
  659.         NULL;
  660.     ((NCR5380_hostdata *) expires_first->host_data)->time_expires = 
  661.         0;
  662.     expires_first = instance;
  663.     }
  664.  
  665.     if (expires_first) {
  666.     timer_table[NCR5380_TIMER].expires = ((NCR5380_hostdata *) 
  667.         expires_first->host_data)->time_expires;
  668.     timer_active |= (1 << NCR5380_TIMER);
  669.     } else {
  670.     timer_table[NCR5380_TIMER].expires = 0;
  671.     timer_active &= ~(1 << MCR5380_TIMER);
  672.     }
  673.     sti();
  674.  
  675.     run_main();
  676. }
  677. #endif /* def USLEEP */
  678.  
  679. static void NCR5380_all_init (void)
  680.  
  681. {
  682.     static int done = 0;
  683.     if (!done) {
  684. #if (NDEBUG & NDEBUG_INIT)
  685.     printk("scsi : NCR5380_all_init()\n");
  686. #endif
  687.     done = 1;
  688. #ifdef USLEEP
  689.     timer_table[NCR5380_TIMER].expires = 0;
  690.     timer_table[NCR5380_TIMER].fn = NCR5380_timer_fn;
  691. #endif
  692.     }
  693. }
  694.  
  695. #ifdef AUTOPROBE_IRQ
  696. /*
  697.  * Function : int NCR5380_probe_irq (struct Scsi_Host *instance, int possible)
  698.  * 
  699.  * Purpose : autoprobe for the IRQ line used by the NCR5380.  
  700.  *
  701.  * Inputs : instance - pointer to this instance of the NCR5380 driver,
  702.  *          possible - bitmask of permissable interrupts.
  703.  *
  704.  * Returns : number of the IRQ selected, IRQ_NONE if no interrupt fired.
  705.  * 
  706.  * XXX no effort is made to deal with spurious interrupts. 
  707.  */
  708.  
  709.  
  710. static int probe_irq;
  711. static void probe_intr (int sig)
  712.  
  713. {
  714.     probe_irq = sig;
  715. };
  716. static struct sigaction probe_sigaction = { probe_intr, 0, SA_INTERRUPT,
  717.     NULL};
  718.  
  719. static int NCR5380_probe_irq (struct Scsi_Host *instance, int possible)
  720.  
  721. {
  722.     NCR5380_local_declare();
  723.     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
  724.      instance->hostdata;
  725.     unsigned long timeout;
  726.     int trying_irqs, i, mask;
  727.     NCR5380_setup(instance);
  728.  
  729.     for (trying_irqs = i = 0, mask = 1; i < 16; ++i, mask <<= 1) 
  730.     if ((mask & possible) &&  (irqaction (i, &probe_sigaction) 
  731.         == 0)) 
  732.         trying_irqs |= mask;
  733.  
  734.     timeout = jiffies + 25;
  735.     probe_irq = IRQ_NONE;
  736.  
  737. /*
  738.  * A interrupt is triggered whenever BSY = false, SEL = true
  739.  * and a bit set in the SELECT_ENABLE_REG is asserted on the 
  740.  * SCSI bus.
  741.  *
  742.  * Note that the bus is only driven when the phase control signals
  743.  * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
  744.  * to zero.
  745.  */
  746.  
  747.     NCR5380_write(TARGET_COMMAND_REG, 0);
  748.     NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  749.     NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
  750.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | 
  751.     ICR_ASSERT_SEL);
  752.  
  753.     while (probe_irq == IRQ_NONE && jiffies < timeout);
  754.  
  755.     NCR5380_write(SELECT_ENABLE_REG, 0);
  756.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  757.  
  758.     for (i = 0, mask = 1; i < 16; ++i, mask <<= 1)
  759.     if (trying_irqs & mask) 
  760.         free_irq(i);
  761.  
  762.     return probe_irq;
  763. }
  764. #endif /* AUTOPROBE_IRQ */
  765.  
  766. /*
  767.  * Function : void NCR58380_print_options (struct Scsi_Host *instance)
  768.  *
  769.  * Purpose : called by probe code indicating the NCR5380 driver
  770.  *         options that were selected.
  771.  *
  772.  * Inputs : instance, pointer to this instance.  Unused.
  773.  */
  774.  
  775. static void NCR5380_print_options (struct Scsi_Host *instance)
  776.  
  777. {
  778.     printk(" generic options"
  779. #ifdef AUTOPROBE_IRQ
  780.     " AUTOPROBE_IRQ"
  781. #endif
  782. #ifdef AUTOSENSE 
  783.     " AUTOSENSE"
  784. #endif
  785. #ifdef DIFFERENTIAL
  786.     " DIFFERENTIAL"
  787. #endif
  788. #ifdef REAL_DMA
  789.     " REAL DMA"
  790. #endif
  791. #ifdef REAL_DMA_POLL
  792.     " REAL DMA POLL"
  793. #endif
  794. #ifdef PARITY
  795.     " PARITY"
  796. #endif
  797. #ifdef PSEUDO_DMA
  798.     " PSEUDO DMA"
  799. #endif
  800. #ifdef SCSI2
  801.     " SCSI-2"
  802. #endif
  803. #ifdef UNSAFE
  804.     " UNSAFE "
  805. #endif
  806.     );
  807. #ifdef USLEEP
  808.     printk(" USLEEP, USLEEP_POLL=%d USLEEP_SLEEP=%d", USLEEP_POLL, USLEEP_SLEEP);
  809. #endif
  810.     printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
  811. }
  812.  
  813.  
  814. /* 
  815.  * Function : void NCR5380_init (struct Scsi_Host *instance)
  816.  *
  817.  * Purpose : initializies *instance and corresponding 5380 chip.
  818.  *
  819.  * Inputs : instance - instantiation of the 5380 driver.  
  820.  *
  821.  * Notes : I assume that the host, hostno, and id bits have been
  822.  *     set correctly.  I don't care about the irq and other fields. 
  823.  * 
  824.  */
  825.  
  826. static void NCR5380_init (struct Scsi_Host *instance)
  827.  
  828. {
  829.     NCR5380_local_declare();
  830.     int i;
  831.     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) 
  832.     instance->hostdata;
  833.     NCR5380_setup(instance);
  834.  
  835.     NCR5380_all_init();
  836.  
  837.     hostdata->id_mask = 1 << instance->this_id;
  838.     for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
  839.     if (i > hostdata->id_mask)
  840.         hostdata->id_higher_mask |= i;
  841.     for (i = 0; i < 8; ++i)
  842.     hostdata->busy[i] = 0;
  843. #ifdef REAL_DMA
  844.     hostdata->dma_len = 0;
  845. #endif
  846.     hostdata->connected = NULL;
  847.     hostdata->issue_queue = NULL;
  848.     hostdata->disconnected_queue = NULL;
  849.     hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT;
  850.  
  851.     if (!the_template) {
  852.     the_template = instance->hostt;
  853.     first_instance = instance;
  854.     }
  855.     
  856.  
  857. #ifdef USLEEP
  858.     hostdata->time_expires = 0;
  859.     hostdata->next_timer = NULL;
  860. #endif
  861.  
  862. #ifndef AUTOSENSE
  863.     if ((instance->cmd_per_lun > 1) || instance->can_queue > 1)) 
  864.      printk("scsi%d : WARNING : support for multiple outstanding commands enabled\n"
  865.             "         without AUTOSENSE option, contigent alligence conditions may\n"
  866.             "         be incorrectly cleared.\n", instance->host_no);
  867. #endif /* def AUTOSENSE */
  868.  
  869.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  870.     NCR5380_write(MODE_REG, MR_BASE);
  871.     NCR5380_write(TARGET_COMMAND_REG, 0);
  872.     NCR5380_write(SELECT_ENABLE_REG, 0);
  873. }
  874.  
  875. /* 
  876.  * Function : int NCR5380_queue_command (Scsi_Cmnd *cmd, 
  877.  *    void (*done)(Scsi_Cmnd *)) 
  878.  *
  879.  * Purpose :  enqueues a SCSI command
  880.  *
  881.  * Inputs : cmd - SCSI command, done - function called on completion, with
  882.  *    a pointer to the command descriptor.
  883.  * 
  884.  * Returns : 0
  885.  *
  886.  * Side effects : 
  887.  *      cmd is added to the per instance issue_queue, with minor 
  888.  *    twiddling done to the host specific fields of cmd.  If the 
  889.  *    main coroutine is not running, it is restarted.
  890.  *
  891.  */
  892.  
  893. /* Only make static if a wrapper function is used */
  894. #ifndef NCR5380_queue_command
  895. static
  896. #endif
  897. int NCR5380_queue_command (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
  898.  
  899. {
  900.     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
  901.     cmd->host->hostdata;
  902.     Scsi_Cmnd *tmp;
  903.  
  904. #if (NDEBUG & NDEBUG_NO_WRITE)
  905.     switch (cmd->cmnd[0]) {
  906.     case WRITE:
  907.     case WRITE_10:
  908.     printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n",
  909.         cmd->host->host_no);
  910.     cmd->result = (DID_ERROR << 16);
  911.     done(cmd);
  912.     return 0;
  913.     }
  914. #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
  915.  
  916.  
  917.     /* 
  918.      * We use the host_scribble field as a pointer to the next command  
  919.      * in a queue 
  920.      */
  921.  
  922.     cmd->host_scribble = NULL;
  923.     cmd->scsi_done = done;
  924.  
  925.     cmd->result = 0;
  926.  
  927.  
  928.     /* 
  929.      * Insert the cmd into the issue queue. Note that REQUEST SENSE 
  930.      * commands are added to the head of the queue since any command will
  931.      * clear the contingent allegience condition that exists and the 
  932.      * sense data is only guranteed to be valid while the condition exists.
  933.      */
  934.  
  935.     cli();
  936.     if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
  937.     cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
  938.     hostdata->issue_queue = cmd;
  939.     } else {
  940.     for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->host_scribble; 
  941.         tmp = (Scsi_Cmnd *) tmp->host_scribble);
  942.     tmp->host_scribble = (unsigned char *) cmd;
  943.     }
  944. /*    sti(); */
  945. #if (NDEBUG & NDEBUG_QUEUES)
  946.     printk("scsi%d : command added to %s of queue\n", cmd->host->host_no,
  947.     (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
  948. #endif
  949.  
  950. /* Run the coroutine if it isn't allready running. */
  951.     run_main();
  952.     return 0;
  953. }
  954.  
  955. /*
  956.  * Function : NCR5380_main (void) 
  957.  *
  958.  * Purpose : NCR5380_main is a corroutine that runs as long as more work can 
  959.  *    be done on the NCR5380 host adapters in a system.  Both 
  960.  *    NCR5380_queue_command() and NCR5380_intr() will try to start it 
  961.  *    in case it is not running.
  962.  * 
  963.  * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should 
  964.  *  reenable them.  This prevents rentrancy and kernel stack overflow.
  965.  */     
  966.     
  967. static void NCR5380_main (void)
  968.  
  969. {
  970.     Scsi_Cmnd *tmp, *prev;
  971.     struct Scsi_Host *instance;
  972.     struct NCR5380_hostdata *hostdata;
  973.     int done;
  974.  
  975.     /*
  976.      * We run (with interrupts disabled) until we're sure that none of 
  977.      * the host adapters have anything that can be done, at which point 
  978.      * we set main_running to 0 and exit.
  979.      *
  980.      * Interrupts are enabled before doing various other internal 
  981.      * instructions, after we've decided that we need to run through
  982.      * the loop again.
  983.      *
  984.      * this should prevent any race conditions.
  985.      */
  986.  
  987.     do {
  988.     cli(); /* Freeze request queues */
  989.     done = 1;
  990.     for (instance = first_instance; instance && instance->hostt == the_template; 
  991.         instance=instance->next) {
  992.         hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  993.         cli();
  994.         if (!hostdata->connected) {
  995. #if (NDEBUG & NDEBUG_MAIN)
  996.         printk("scsi%d : not connected\n", instance->host_no);
  997. #endif
  998.         /*
  999.          * Search through the issue_queue for a command destined
  1000.          * for a target that's not busy.
  1001.          */
  1002.         for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, 
  1003.             prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) 
  1004.             tmp->host_scribble) 
  1005.  
  1006.             /*  When we find one, remove it from the issue queue. */
  1007.             if (!(hostdata->busy[tmp->target] & (1 << tmp->lun))) {
  1008.                 if (prev)
  1009.                 prev->host_scribble = tmp->host_scribble;
  1010.                 else
  1011.                 hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble;
  1012.             tmp->host_scribble = NULL;
  1013.  
  1014.             /* renable interrupts after finding one */
  1015.             CHECK_ICNT("main anbling ints before selecting");
  1016.             sti();
  1017.  
  1018.             /* 
  1019.              * Attempt to establish an I_T_L nexus here. 
  1020.              * On success, instance->hostdata->connected is set.
  1021.              * On failure, we must add the command back to the
  1022.              *   issue queue so we can keep trying.    
  1023.              */
  1024. #if (NDEBUG & (NDEBUG_MAIN | NDEBUG_QUEUES))
  1025.             printk("scsi%d : main() : command for target %d lun %d removed from issue_queue\n",
  1026.                 instance->host_no, tmp->target, tmp->lun);
  1027. #endif
  1028.                 /* 
  1029.                  * REQUEST SENSE commands are issued without tagged
  1030.                  * queueing, even on SCSI-II devices because the 
  1031.                  * contingent alligence condition exists for the 
  1032.                  * entire unit.
  1033.                  */
  1034.                 
  1035.             if (!NCR5380_select(instance, tmp, 
  1036.                 (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : 
  1037.                 TAG_NEXT)) {
  1038.                 break;
  1039.             } else {
  1040.                 cli();
  1041.                 tmp->host_scribble = (unsigned char *) 
  1042.                 hostdata->issue_queue;
  1043.                 hostdata->issue_queue = tmp;
  1044.                 sti();
  1045. #if (NDEBUG & (NDEBUG_MAIN | NDEBUG_QUEUES))
  1046.             printk("scsi%d : main(): select() failed, returned to issue_queue\n",
  1047.                 instance->host_no);
  1048. #endif
  1049.             }
  1050.             } /* if target/lun is not busy */
  1051.         } /* if (!hostdata->connected) */
  1052.         
  1053.         if (hostdata->connected 
  1054. #ifdef REAL_DMA
  1055.         && !hostdata->dma_len
  1056. #endif
  1057. #ifdef USLEEP
  1058.         && (!hostdata->time_expires || hostdata->time_expires >= jiffies)
  1059. #endif
  1060.         ) {
  1061.         CHECK_ICNT("main enabling ints before inform_transf");
  1062.         sti();
  1063. #if (NDEBUG & NDEBUG_MAIN)
  1064.         printk("scsi%d : main() : performing information transfer\n",
  1065.             instance->host_no);
  1066. #endif
  1067.         NCR5380_information_transfer(instance);
  1068. #if (NDEBUG & NDEBUG_MAIN)
  1069.         printk("scsi%d : main() : done set false\n", instance->host_no);
  1070. #endif
  1071.         CHECK_ICNT("main finish with inform_transf");
  1072.         done = 0;
  1073.         } else 
  1074.         break;
  1075.     } /* for instance */
  1076.     } while (!done);
  1077.     main_running = 0;
  1078. }
  1079.  
  1080.  
  1081. /*
  1082.  * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
  1083.  *
  1084.  * Purpose : Called by interrupt handler when DMA finishes or a phase
  1085.  *    mismatch occurs (which would finish the DMA transfer).  
  1086.  *
  1087.  * Inputs : instance - this instance of the NCR5380.
  1088.  *
  1089.  */
  1090.  
  1091. static void NCR5380_dma_complete( struct Scsi_Host *instance )
  1092.  
  1093. {   struct NCR5380_hostdata *hostdata =
  1094.     (struct NCR5380_hostdata *)instance->hostdata;
  1095.     int           transfered;
  1096.     unsigned char **data, p, basr;
  1097.     volatile int  *count;
  1098. #ifdef READ_OVERRUNS
  1099.     int              saved_data = 0, overrun = 0, cnt, toPIO;
  1100. #else
  1101.     unsigned char sr;
  1102. #endif
  1103.     
  1104.     if (!hostdata->connected) 
  1105.     panic("scsi%d : recieved end of DMA interrupt with no connected cmd\n",
  1106.           instance->host_no);
  1107.  
  1108.     p = hostdata->connected->SCp.phase;
  1109.     if (p & SR_IO) {
  1110. #ifdef READ_OVERRUNS
  1111.     udelay(10);
  1112.     if ((((basr = NCR5380_read(BUS_AND_STATUS_REG)) &
  1113.           (BASR_PHASE_MATCH|BASR_ACK)) == (BASR_PHASE_MATCH | BASR_ACK))) {
  1114.         saved_data = NCR5380_read(INPUT_DATA_REG);
  1115.         overrun = 1;
  1116. #if I_HAVE_OVERRUNS == 1
  1117.         /* Overrun probe mode: print a message that a overrun occured. */
  1118.         printk( "SCSI: overrun handled (better set I_HAVE_OVERRUNS to 2 in drivers/scsi/atari_scsi.h)\n" );
  1119. #endif
  1120.     }
  1121. #endif /* READ_OVERRUNS */
  1122.     }
  1123.     else {
  1124.     int limit = 100;
  1125.     while (((basr = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) ||
  1126.            (NCR5380_read(STATUS_REG) & SR_REQ)) {
  1127.         if (!(basr & BASR_PHASE_MATCH)) break;
  1128.         if (--limit < 0) break;
  1129.     }
  1130.     }
  1131.     
  1132. #if (NDEBUG & NDEBUG_DMA)
  1133.     printk( "scsi%d : real DMA transfer complete, basr 0x%X, sr 0x%X\n",
  1134.         instance->host_no, basr, NCR5380_read(STATUS_REG));
  1135. #endif
  1136.     
  1137.     (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1138. #if !defined(READ_OVERRUNS)
  1139.     while( NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK) ;
  1140. #endif
  1141.     NCR5380_write(MODE_REG, MR_BASE);
  1142.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1143.  
  1144.     transfered = hostdata->dma_len - NCR5380_dma_residual(instance);
  1145.     hostdata->dma_len = 0;
  1146.                 
  1147.     data = (unsigned char **) &(hostdata->connected->SCp.ptr);
  1148.     count = &(hostdata->connected->SCp.this_residual);
  1149.     *data += transfered;
  1150.     *count -= transfered;
  1151.  
  1152. #if !defined(READ_OVERRUNS)
  1153.     /* After turning off the DMA, a phase mismatch might occur. This
  1154.      * would cause another interrupt. But because DMA mode is already
  1155.      * cleared, NCR5380_intr() would report an unknown interrupt. To
  1156.      * avoid these messages, wait for REQ to be asserted again or for
  1157.      * an explicit phase mismatch. If REQ is asserted and the bus is
  1158.      * still in phase, no more interrupt will come.
  1159.      */
  1160.     while(!((sr=NCR5380_read(STATUS_REG)) & SR_REQ) && (sr & PHASE_MASK) == p)
  1161.     ;
  1162.     if ((sr & PHASE_MASK) != p) {
  1163.     (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1164.     /* Note that the pending bit in the MFP must be cleared.
  1165.      * Otherwise the interrupt get delivered without the IRQ bit
  1166.      * set in the BASR.
  1167.      */
  1168.     if (IS_A_TT())
  1169.         tt_mfp.int_pn_a &= ~0x80;
  1170.     else
  1171.         mfp.int_pn_b &= ~0x80;
  1172.     }
  1173. #endif /* !READ_OVERRUNS */
  1174.  
  1175.     /* Now it save to turn on interrupts -- and it's better here in
  1176.      * case a slow PIO is done.
  1177.      */
  1178.     CHECK_ICNT("dma_complete enabling ints");
  1179.     sti();
  1180.     
  1181. #ifdef READ_OVERRUNS
  1182.     if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
  1183.     if (overrun) {
  1184. #if (NDEBUG & NDEBUG_DMA)
  1185.         printk("Got an input overrun, using saved byte\n");
  1186. #endif    
  1187.         *(*data)++ = saved_data;
  1188.         (*count)--;
  1189.         cnt = toPIO = 1;
  1190.     }
  1191.     else {
  1192.         cnt = toPIO = 2;
  1193.     }
  1194. #if (NDEBUG & NDEBUG_DMA)
  1195.     printk( "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data );
  1196. #endif
  1197.     NCR5380_transfer_pio(instance, &p, &cnt, data);
  1198.     *count -= toPIO - cnt;
  1199.     }
  1200. #endif /* READ_OVERRUNS */
  1201.  
  1202. }
  1203.  
  1204.  
  1205. /*
  1206.  * Function : void NCR5380_intr (int irq)
  1207.  * 
  1208.  * Purpose : handle interrupts, restablishing I_T_L or I_T_L_Q nexuses
  1209.  *    from the disconnected queue, and restarting NCR5380_main() 
  1210.  *    as required.
  1211.  *
  1212.  * Inputs : int irq, irq that caused this interrupt.
  1213.  *
  1214.  */
  1215.  
  1216. static void NCR5380_intr (int irq)
  1217.  
  1218. {
  1219.     NCR5380_local_declare(); 
  1220.     struct Scsi_Host *instance;
  1221.     struct NCR5380_hostdata *hostdata;
  1222.     int done;
  1223.     unsigned char basr;
  1224. #if (NDEBUG & NDEBUG_INTR)
  1225.     printk("scsi : NCR5380 irq %d triggered\n", irq);
  1226. #endif
  1227.     do {
  1228.         done = 1;
  1229.         for (instance = first_instance; instance && (instance->hostt == 
  1230.          the_template); instance = instance->next) {
  1231.         if (instance->irq == irq) {
  1232.  
  1233.         hostdata = (struct NCR5380_hostdata *)instance->hostdata;
  1234.         /* Look for pending interrupts */
  1235.         NCR5380_setup(instance);
  1236.         basr = NCR5380_read(BUS_AND_STATUS_REG);
  1237. #if (NDEBUG & NDEBUG_INTR)
  1238.         printk( "scsi%d: BASR=%02x\n", instance->host_no, basr );
  1239. #endif
  1240.         /* XXX dispatch to appropriate routine if found and done=0 */
  1241.         if (basr & BASR_IRQ) {
  1242. #if (NDEBUG & NDEBUG_INTR)
  1243.             NCR5380_print(instance);
  1244. #endif
  1245.             if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == 
  1246.             (SR_SEL | SR_IO)) {
  1247.             done = 0;
  1248.             sti();
  1249. #if (NDEBUG & NDEBUG_INTR)
  1250.             printk("scsi%d : SEL interrupt\n", instance->host_no);
  1251. #endif
  1252.             NCR5380_reselect(instance);
  1253.             (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1254.             } else if (basr & BASR_PARITY_ERROR) {
  1255. #if (NDEBUG & NDEBUG_INTR)
  1256.             printk("scsi%d : PARITY interrupt\n", instance->host_no);
  1257. #endif
  1258.             (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1259.             } else {
  1260. /*  
  1261.  * XXX the rest of the interrupt conditions should *only* occur during a 
  1262.  * DMA transfer, which I haven't gotten arround to fixing yet.
  1263.  */
  1264.  
  1265. #if defined(REAL_DMA)
  1266.             /*
  1267.              * We should only get PHASE MISMATCH and EOP
  1268.              * interrupts if we have DMA enabled, so do a
  1269.              * sanity check based on the current setting
  1270.              * of the MODE register.
  1271.              */
  1272.  
  1273.             if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
  1274.                 ((basr & BASR_END_DMA_TRANSFER) || 
  1275.                  !(basr & BASR_PHASE_MATCH))) {
  1276.  
  1277. #if (NDEBUG & NDEBUG_INTR)
  1278.                 printk("scsi%d : PHASE MISM or EOP interrupt\n",
  1279.                    instance->host_no);
  1280. #endif
  1281.                 NCR5380_dma_complete( instance );
  1282.                 done = 0;
  1283.             } else
  1284. #endif /* REAL_DMA */
  1285.                    {
  1286. #if 1 || (NDEBUG & NDEBUG_INTR)
  1287.                 printk("scsi : unknown interrupt, BASR 0x%X, MR 0x%X, SR 0x%x\n",
  1288.                    basr, NCR5380_read(MODE_REG),
  1289.                    NCR5380_read(STATUS_REG));
  1290. #endif
  1291.                 (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1292.             }
  1293.             } /* if !(SELECTION || PARITY) */
  1294.         } /* BASR & IRQ */
  1295.         else {
  1296. #if 1 || (NDEBUG & NDEBUG_INTR)
  1297.             printk("scsi : interrupt without IRQ bit set in BASR, BASR 0x%X, MR 0x%X, SR 0x%x\n", basr, NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
  1298. #endif
  1299.             (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1300.         }
  1301.         } /* instance->irq == irq */
  1302.     } /* for instances */
  1303.     if (!done) {
  1304. #if (NDEBUG & NDEBUG_INTR)
  1305.         printk("scsi%d : in int routine: not done, calling main\n",
  1306.            instance->host_no);
  1307. #endif
  1308.         CHECK_ICNT("NCR5380_intr calling main");
  1309.         run_main();
  1310.         /* ++roman: After running main, interrupts are enabled and a
  1311.          * new DMA process may have been started. So we should leave
  1312.          * this instance of interrupt service routine! Otherwise
  1313.          * recursive calls may happen.
  1314.          */
  1315.         return;
  1316.     } /* !done */
  1317.     } while (0);
  1318. }
  1319.  
  1320. /* 
  1321.  * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd, 
  1322.  *    int tag);
  1323.  *
  1324.  * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
  1325.  *    including ARBITRATION, SELECTION, and initial message out for 
  1326.  *    IDENTIFY and queue messages. 
  1327.  *
  1328.  * Inputs : instance - instantiation of the 5380 driver on which this 
  1329.  *     target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for 
  1330.  *    new tag, TAG_NONE for untagged queueing, otherwise set to the tag for 
  1331.  *    the command that is presently connected.
  1332.  * 
  1333.  * Returns : -1 if selection could not execute for some reason,
  1334.  *    0 if selection succeeeded or failed because the target 
  1335.  *     did not respond.
  1336.  *
  1337.  * Side effects : 
  1338.  *     If bus busy, arbitration failed, etc, NCR5380_select() will exit 
  1339.  *        with registers as they should have been on entry - ie
  1340.  *        SELECT_ENABLE will be set appropriately, the NCR5380
  1341.  *        will cease to drive any SCSI bus signals.
  1342.  *
  1343.  *    If successful : I_T_L or I_T_L_Q nexus will be established, 
  1344.  *        instance->connected will be set to cmd.  
  1345.  *         SELECT interrupt will be disabled.
  1346.  *
  1347.  *    If failed (no target) : cmd->scsi_done() will be called, and the 
  1348.  *        cmd->result host byte set to DID_BAD_TARGET.
  1349.  */
  1350.  
  1351. static int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd, int tag)
  1352.  
  1353. {
  1354.     NCR5380_local_declare();
  1355.     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata*) 
  1356.     instance->hostdata;
  1357.     unsigned char tmp[3], phase;
  1358.     unsigned char *data;
  1359.     int len;
  1360.     unsigned long timeout;
  1361.     NCR5380_setup(instance);
  1362.  
  1363. #if defined (NDEBUG) && (NDEBUG & NDEBUG_ARBITRATION) 
  1364.     NCR5380_print(instance);
  1365.     printk("scsi%d : starting arbitration, id = %d\n", instance->host_no,
  1366.     instance->this_id);
  1367. #endif
  1368.  
  1369.     /* 
  1370.      * Set the phase bits to 0, otherwise the NCR5380 won't drive the 
  1371.      * data bus during SELECTION.
  1372.      */
  1373.  
  1374.     NCR5380_write(TARGET_COMMAND_REG, 0);
  1375.  
  1376.     /* Start arbitration */ 
  1377.     NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
  1378.     NCR5380_write(MODE_REG, MR_ARBITRATE);
  1379.  
  1380.     /* Wait for arbitration logic to complete */
  1381.     while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS));
  1382.  
  1383. #if (NDEBUG & NDEBUG_ARBITRATION) 
  1384.     printk("scsi%d : arbitration complete\n", instance->host_no);
  1385. /* Avoid GCC 2.4.5 asm needs to many reloads error */
  1386.     __asm__("nop");
  1387. #endif
  1388.  
  1389.     /* 
  1390.      * The arbitration delay is 2.2us, but this is a minimum and there is 
  1391.      * no maximum so we can safely sleep for ceil(2.2) usecs to accomodate
  1392.      * the integral nature of udelay().
  1393.      *
  1394.      */
  1395.  
  1396.     udelay(3);
  1397.  
  1398.     /* Check for lost arbitration */
  1399.     if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
  1400.     (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
  1401.     (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
  1402.     NCR5380_write(MODE_REG, MR_BASE); 
  1403. #if (NDEBUG & NDEBUG_ARBITRATION)
  1404.     printk("scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", 
  1405.     instance->host_no);
  1406. #endif
  1407.     return -1;
  1408.     }
  1409.  
  1410.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL);
  1411.     
  1412.     if (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) {
  1413.     NCR5380_write(MODE_REG, MR_BASE);
  1414.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1415. #if (NDEBUG & NDEBUG_ARBITRATION)
  1416.     printk("scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n", 
  1417.     instance->host_no);
  1418. #endif
  1419.     return -1;
  1420.     }
  1421.  
  1422.     /* 
  1423.      * Again, bus clear + bus settle time is 1.2us, however, this is 
  1424.      * a minimum so we'll udelay ceil(1.2)
  1425.      */
  1426.  
  1427.     udelay(2);    
  1428.  
  1429. #if (NDEBUG & NDEBUG_ARBITRATION)
  1430.     printk("scsi%d : won arbitration\n", instance->host_no);
  1431. #endif
  1432.  
  1433.     /* 
  1434.      * Now that we have won arbitration, start Selection process, asserting 
  1435.      * the host and target ID's on the SCSI bus.
  1436.      */
  1437.  
  1438.     NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->target)));
  1439.  
  1440.     /* 
  1441.      * Raise ATN while SEL is true before BSY goes false from arbitration,
  1442.      * since this is the only way to gurantee that we'll get a MESSAGE OUT
  1443.      * phase immediately after selection.
  1444.      */
  1445.  
  1446.     NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | 
  1447.     ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
  1448.     NCR5380_write(MODE_REG, MR_BASE);
  1449.  
  1450.     /* 
  1451.      * Reselect interrupts must be turned off prior to the dropping of BSY,
  1452.      * otherwise we will trigger an interrupt.
  1453.      */
  1454.     NCR5380_write(SELECT_ENABLE_REG, 0);
  1455.  
  1456.     /* Reset BSY */
  1457.     NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | 
  1458.     ICR_ASSERT_ATN | ICR_ASSERT_SEL));
  1459.  
  1460.     /* 
  1461.      * Something wierd happens when we cease to drive BSY - looks
  1462.      * like the board/chip is letting us do another read before the 
  1463.      * appropriate propogation delay has expired, and we're confusing
  1464.      * a BSY signal from ourselves as the target's response to SELECTION.
  1465.      *
  1466.      * A small delay (the 'C++' frontend breaks the pipeline with an
  1467.      * unecessary jump, making it work on my 386-33/Trantor T128, the
  1468.      * tighter 'C' code breaks and requires this) solves the problem - 
  1469.      * the 1 us delay is arbitrary, and only used because this delay will 
  1470.      * be the same on other platforms and since it works here, it should 
  1471.      * work there.
  1472.      */
  1473.  
  1474.     udelay(1);
  1475.  
  1476. #if (NDEBUG & NDEBUG_SELECTION)
  1477.     printk("scsi%d : selecting target %d\n", instance->host_no, cmd->target);
  1478. #endif
  1479.  
  1480.     /* 
  1481.      * The SCSI specification calls for a 250 ms timeout for the actual 
  1482.      * selection.
  1483.      */
  1484.  
  1485.     timeout = jiffies + 25; 
  1486.  
  1487.     /* 
  1488.      * XXX very interesting - we're seeing a bounce where the BSY we 
  1489.      * asserted is being reflected / still asserted (propogation delay?)
  1490.      * and it's detecting as true.  Sigh.
  1491.      */
  1492.  
  1493.     while ((jiffies < timeout) && !(NCR5380_read(STATUS_REG) & SR_BSY));
  1494.  
  1495.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1496.  
  1497.     if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
  1498.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1499.     cmd->result = DID_BAD_TARGET << 16;
  1500.     cmd->scsi_done(cmd);
  1501.     NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1502. #if (NDEBUG & NDEBUG_SELECTION)
  1503.     printk("scsi%d : target did not respond within 250ms\n", 
  1504.         instance->host_no);
  1505. #endif
  1506.     return 0;
  1507.     } 
  1508.  
  1509.     /*
  1510.      * Since we followed the SCSI spec, and raised ATN while SEL 
  1511.      * was true but before BSY was false during selection, the information
  1512.      * transfer phase should be a MESSAGE OUT phase so that we can send the
  1513.      * IDENTIFY message.
  1514.      * 
  1515.      * If SCSI-II tagged queing is enabled, we also send a SIMPLE_QUEUE_TAG
  1516.      * message (2 bytes) with a tag ID that we increment with every command
  1517.      * until it wraps back to 0.
  1518.      *
  1519.      * XXX - it turns out that there are some broken SCSI-II devices,
  1520.      *         which claim to support tagged queing but fail when more than
  1521.      *         some number of commands are issued at once.
  1522.      */
  1523.  
  1524.     /* Wait for start of REQ/ACK handshake */
  1525.     while (!(NCR5380_read(STATUS_REG) & SR_REQ));
  1526.  
  1527. #if (NDEBUG & NDEBUG_SELECTION)
  1528.     printk("scsi%d : target %d selected, going into MESSAGE OUT phase.\n",
  1529.     instance->host_no, cmd->target);
  1530. #endif
  1531.     tmp[0] = IDENTIFY(((instance->irq == IRQ_NONE) ? 0 : 1), cmd->lun);
  1532. #ifdef SCSI2
  1533.     if (scsi_devices[cmd->index].tagged_queue && (tag != TAG_NONE)) {
  1534.     tmp[1] = SIMPLE_QUEUE_TAG;
  1535.     if (tag == TAG_NEXT) {
  1536.         /* 0 is TAG_NONE, used to imply no tag for this command */
  1537.         if (scsi_devices[cmd->index].current_tag == 0)
  1538.         scsi_devices[cmd->index].current_tag = 1;
  1539.  
  1540.         cmd->tag = scsi_devices[cmd->index].current_tag;
  1541.         scsi_devices[cmd->index].current_tag++;
  1542.     } else  
  1543.         cmd->tag = (unsigned char) tag;
  1544.  
  1545.     tmp[2] = cmd->tag;
  1546.     hostdata->last_message = SIMPLE_QUEUE_TAG;
  1547.     len = 3;
  1548.     } else 
  1549. #endif /* def SCSI2 */
  1550.     {
  1551.     len = 1;
  1552.     cmd->tag=0;
  1553.     }
  1554.  
  1555.     /* Send message(s) */
  1556.     data = tmp;
  1557.     phase = PHASE_MSGOUT;
  1558.     NCR5380_transfer_pio(instance, &phase, &len, &data);
  1559. #if (NDEBUG & NDEBUG_SELECTION)
  1560.     printk("scsi%d : nexus established.\n", instance->host_no);
  1561. #endif
  1562.     /* XXX need to handle errors here */
  1563.     hostdata->connected = cmd;
  1564. #ifdef SCSI2
  1565.     if (!scsi_devices[cmd->index].tagged_queue)
  1566. #endif    
  1567.     hostdata->busy[cmd->target] |= (1 << cmd->lun);
  1568.  
  1569.     initialize_SCp(cmd);
  1570.  
  1571.     return 0;
  1572. }
  1573.  
  1574. /* 
  1575.  * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance, 
  1576.  *      unsigned char *phase, int *count, unsigned char **data)
  1577.  *
  1578.  * Purpose : transfers data in given phase using polled I/O
  1579.  *
  1580.  * Inputs : instance - instance of driver, *phase - pointer to 
  1581.  *    what phase is expected, *count - pointer to number of 
  1582.  *    bytes to transfer, **data - pointer to data pointer.
  1583.  * 
  1584.  * Returns : -1 when different phase is enterred without transfering
  1585.  *    maximum number of bytes, 0 if all bytes or transfered or exit
  1586.  *    is in same phase.
  1587.  *
  1588.  *     Also, *phase, *count, *data are modified in place.
  1589.  *
  1590.  * XXX Note : handling for bus free may be useful.
  1591.  */
  1592.  
  1593. /*
  1594.  * Note : this code is not as quick as it could be, however it 
  1595.  * IS 100% reliable, and for the actual data transfer where speed
  1596.  * counts, we will always do a pseudo DMA or DMA transfer.
  1597.  */
  1598.  
  1599. static int NCR5380_transfer_pio( struct Scsi_Host *instance, 
  1600.                  unsigned char *phase, int *count,
  1601.                  unsigned char **data)
  1602.  
  1603. {
  1604.     NCR5380_local_declare();
  1605.     register unsigned char p = *phase, tmp;
  1606.     register int c = *count;
  1607.     register unsigned char *d = *data;
  1608.     NCR5380_setup(instance);
  1609.  
  1610.     /* 
  1611.      * The NCR5380 chip will only drive the SCSI bus when the 
  1612.      * phase specified in the appropriate bits of the TARGET COMMAND
  1613.      * REGISTER match the STATUS REGISTER
  1614.      */
  1615.  
  1616.     NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
  1617.  
  1618.     do {
  1619.     /* 
  1620.      * Wait for assertion of REQ, after which the phase bits will be 
  1621.      * valid 
  1622.      */
  1623.     while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ));
  1624.  
  1625. #if (NDEBUG & NDEBUG_HANDSHAKE)
  1626.     printk("scsi%d : REQ detected\n", instance->host_no);
  1627. #endif
  1628.  
  1629.     /* Check for phase mismatch */    
  1630.     if ((tmp & PHASE_MASK) != p) {
  1631. #if (NDEBUG & NDEBUG_PIO)
  1632.         printk("scsi%d : phase mismatch\n", instance->host_no);
  1633.         NCR5380_print_phase(instance);
  1634. #endif
  1635.         break;
  1636.     }
  1637.  
  1638.     /* Do actual transfer from SCSI bus to / from memory */
  1639.     if (!(p & SR_IO)) 
  1640.         NCR5380_write(OUTPUT_DATA_REG, *d);
  1641.     else 
  1642.         *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
  1643.  
  1644.     ++d;
  1645.  
  1646.     /* 
  1647.      * The SCSI standard suggests that in MSGOUT phase, the initiator
  1648.      * should drop ATN on the last byte of the message phase
  1649.      * after REQ has been asserted for the handshake but before
  1650.      * the initiator raises ACK.
  1651.      */
  1652.  
  1653.     if (!(p & SR_IO)) {
  1654.         if (!((p & SR_MSG) && c > 1)) {
  1655.         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
  1656.             ICR_ASSERT_DATA);
  1657. #if (NDEBUG & NDEBUG_PIO)
  1658.     NCR5380_print(instance);
  1659. #endif
  1660.         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
  1661.             ICR_ASSERT_DATA | ICR_ASSERT_ACK);
  1662.         } else {
  1663.         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
  1664.             ICR_ASSERT_DATA | ICR_ASSERT_ATN);
  1665. #if (NDEBUG & NDEBUG_PIO)
  1666.     NCR5380_print(instance);
  1667. #endif
  1668.         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
  1669.             ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
  1670.         }
  1671.     } else {
  1672. #if (NDEBUG & NDEBUG_PIO)
  1673.     NCR5380_print(instance);
  1674. #endif
  1675.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
  1676.     }
  1677.  
  1678.     while (NCR5380_read(STATUS_REG) & SR_REQ);
  1679.  
  1680. #if (NDEBUG & NDEBUG_HANDSHAKE)
  1681.         printk("scsi%d : req false, handshake complete\n", instance->host_no);
  1682. #endif
  1683.  
  1684.     if (!(p == PHASE_MSGOUT && c > 1))
  1685.         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1686.     else
  1687.         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1688.     } while (--c);
  1689.  
  1690. #if (NDEBUG & NDEBUG_PIO) 
  1691.     printk("scsi%d : residual %d\n", instance->host_no, c);
  1692. #endif
  1693.  
  1694.     *count = c;
  1695.     *data = d;
  1696.     tmp = NCR5380_read(STATUS_REG);
  1697.     if (tmp & SR_REQ)
  1698.     *phase = tmp & PHASE_MASK;
  1699.     else 
  1700.     *phase = PHASE_UNKNOWN;
  1701.  
  1702.     CHECK_ICNT("leaving transf_PIO");
  1703.     if (!c || (*phase == p))
  1704.     return 0;
  1705.     else 
  1706.     return -1;
  1707. }
  1708.  
  1709. #if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
  1710. /* 
  1711.  * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance, 
  1712.  *      unsigned char *phase, int *count, unsigned char **data)
  1713.  *
  1714.  * Purpose : transfers data in given phase using either real
  1715.  *    or pseudo DMA.
  1716.  *
  1717.  * Inputs : instance - instance of driver, *phase - pointer to 
  1718.  *    what phase is expected, *count - pointer to number of 
  1719.  *    bytes to transfer, **data - pointer to data pointer.
  1720.  * 
  1721.  * Returns : -1 when different phase is enterred without transfering
  1722.  *    maximum number of bytes, 0 if all bytes or transfered or exit
  1723.  *    is in same phase.
  1724.  *
  1725.  *     Also, *phase, *count, *data are modified in place.
  1726.  *
  1727.  */
  1728.  
  1729.  
  1730. static int NCR5380_transfer_dma( struct Scsi_Host *instance, 
  1731.                  unsigned char *phase, int *count,
  1732.                  unsigned char **data)
  1733.  
  1734. {
  1735.     NCR5380_local_declare();
  1736.     register int c = *count;
  1737.     register unsigned char p = *phase;
  1738.     register unsigned char *d = *data;
  1739.     unsigned char tmp;
  1740. #if defined(REAL_DMA_POLL)
  1741.     int cnt, toPIO;
  1742.     unsigned char saved_data = 0, overrun = 0, residue;
  1743. #endif
  1744.  
  1745.     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) 
  1746.     instance->hostdata;
  1747.  
  1748.     NCR5380_setup(instance);
  1749.  
  1750.     if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
  1751.         *phase = tmp;
  1752.         return -1;
  1753.     }
  1754. #if defined(REAL_DMA) || defined(REAL_DMA_POLL) 
  1755. #ifdef READ_OVERRUNS
  1756.      if (p & SR_IO) {
  1757.        c -= 2;
  1758.      }
  1759. #endif /* READ_OVERRUNS */
  1760. #if (NDEBUG & NDEBUG_DMA)
  1761.     printk("scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n",
  1762.     instance->host_no, instance->dma_channel, (p & SR_IO) ? "reading" :
  1763.     "writing", c, (p & SR_IO) ? "to" : "from", (unsigned) d);
  1764. #endif
  1765.     hostdata->dma_len = (p & SR_IO) ?
  1766.     NCR5380_dma_read_setup(instance, d, c) : 
  1767.     NCR5380_dma_write_setup(instance, d, c);
  1768. #endif
  1769.  
  1770.     NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
  1771.  
  1772. #ifdef REAL_DMA
  1773.     NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
  1774. #elif defined(REAL_DMA_POLL)
  1775.     NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
  1776. #else /* if PSEUDO_DMA */
  1777.     /*
  1778.      * Note : on my sample board, watch-dog timeouts occured when interrupts
  1779.      * were not disabled for the duration of a single DMA transfer, from 
  1780.      * before the setting of DMA mode to after transfer of the last byte.
  1781.      */
  1782.  
  1783. #if defined(PSEUDO_DMA) && !defined(UNSAFE)
  1784.     cli();
  1785. #endif
  1786.     NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
  1787. #endif /* def REAL_DMA ^ REAL_DMA_POLL ^ PSEUDO_DMA  */
  1788.  
  1789. #if (NDEBUG & NDEBUG_DMA) & 0
  1790.     printk("scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG));
  1791. #endif
  1792.  
  1793.     if (p & SR_IO)
  1794.     NCR5380_write(START_DMA_INITIATOR_RECIEVE_REG, 0);
  1795.     else {
  1796.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
  1797.     NCR5380_write(START_DMA_SEND_REG, 0);
  1798.     }
  1799.  
  1800. #if defined(REAL_DMA_POLL)
  1801.     do {
  1802.     tmp = NCR5380_read(BUS_AND_STATUS_REG);
  1803.     } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | 
  1804.     BASR_END_DMA_TRANSFER)));
  1805.  
  1806. /*
  1807.   At this point, either we've completed DMA, or we have a phase mismatch,
  1808.   or we've unexpectedly lost BUSY (which is a real error).
  1809.  
  1810.   For write DMAs, we want to wait until the last byte has been
  1811.   transferred out over the bus before we turn off DMA mode.  Alas, there
  1812.   seems to be no terribly good way of doing this on a 5380 under all
  1813.   conditions.  For non-scatter-gather operations, we can wait until REQ
  1814.   and ACK both go false, or until a phase mismatch occurs.  Gather-writes
  1815.   are nastier, since the device will be expecting more data than we
  1816.   are prepared to send it, and REQ will remain asserted.  On a 53C8[01] we
  1817.   could test LAST BIT SENT to assure transfer (I imagine this is precisely
  1818.   why this signal was added to the newer chips) but on the older 538[01]
  1819.   this signal does not exist.  The workaround for this lack is a watchdog;
  1820.   we bail out of the wait-loop after a modest amount of wait-time if
  1821.   the usual exit conditions are not met.  Not a terribly clean or
  1822.   correct solution :-%
  1823.  
  1824.   Reads are equally tricky due to a nasty characteristic of the NCR5380.
  1825.   If the chip is in DMA mode for an READ, it will respond to a target's
  1826.   REQ by latching the SCSI data into the INPUT DATA register and asserting
  1827.   ACK, even if it has _already_ been notified by the DMA controller that
  1828.   the current DMA transfer has completed!  If the NCR5380 is then taken
  1829.   out of DMA mode, this already-acknowledged byte is lost.
  1830.  
  1831.   This is not a problem for "one DMA transfer per command" reads, because
  1832.   the situation will never arise... either all of the data is DMA'ed
  1833.   properly, or the target switches to MESSAGE IN phase to signal a
  1834.   disconnection (either operation bringing the DMA to a clean halt).
  1835.   However, in order to handle scatter-reads, we must work around the
  1836.   problem.  The chosen fix is to DMA N-2 bytes, then check for the
  1837.   condition before taking the NCR5380 out of DMA mode.  One or two extra
  1838.   bytes are tranferred via PIO as necessary to fill out the original
  1839.   request.
  1840. */
  1841.  
  1842.     if (p & SR_IO) {
  1843. #ifdef READ_OVERRUNS
  1844.       udelay(10);
  1845.       if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH|BASR_ACK)) ==
  1846.            (BASR_PHASE_MATCH | BASR_ACK))) {
  1847.         saved_data = NCR5380_read(INPUT_DATA_REGISTER);
  1848.         overrun = 1;
  1849.       }
  1850. #endif /* READ_OVERRUNS */
  1851.     } else {
  1852.       int limit = 100;
  1853.       while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) ||
  1854.             (NCR5380_read(STATUS_REG) & SR_REQ)) {
  1855.         if (!(tmp & BASR_PHASE_MATCH)) break;
  1856.         if (--limit < 0) break;
  1857.       }
  1858.     }
  1859.  
  1860.  
  1861. #if (NDEBUG & NDEBUG_DMA)
  1862.     printk("scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n",
  1863.        instance->host_no, tmp, NCR5380_read(STATUS_REG));
  1864. #endif
  1865.  
  1866.     NCR5380_write(MODE_REG, MR_BASE);
  1867.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1868.  
  1869.     residue = NCR5380_dma_residual(instance);
  1870.     c -= residue;
  1871.     *count -= c;
  1872.     *data += c;
  1873.     *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
  1874.  
  1875. #ifdef READ_OVERRUNS
  1876.     if (*phase == p && (p & SR_IO) && residue == 0) {
  1877.       if (overrun) {
  1878. #if (NDEBUG & NDEBUG_DMA)
  1879.         printk("Got an input overrun, using saved byte\n");
  1880. #endif
  1881.         **data = saved_data;
  1882.         *data += 1;
  1883.         *count -= 1;
  1884.         cnt = toPIO = 1;
  1885.       } else {
  1886.         printk("No overrun??\n");
  1887.         cnt = toPIO = 2;
  1888.       }
  1889. #if (NDEBUG & NDEBUG_DMA)
  1890.       printk("Doing %d-byte PIO to 0x%X\n", cnt, *data);
  1891. #endif
  1892.       NCR5380_transfer_pio(instance, phase, &cnt, data);
  1893.       *count -= toPIO - cnt;
  1894.     }
  1895. #endif /* READ_OVERRUNS */
  1896.  
  1897. #if (NDEBUG & NDEBUG_DMA)
  1898.      printk("Return with data ptr = 0x%X, count %d, last 0x%X, next 0x%X\n",
  1899.         *data, *count, *(*data+*count-1), *(*data+*count));
  1900. #endif
  1901.      return 0;
  1902.      
  1903. #elif defined(REAL_DMA)
  1904.     CHECK_ICNT("leaving transf_dma");
  1905.     return 0;
  1906. #else /* defined(REAL_DMA_POLL) => PSEUDO_DMA */
  1907.     if (p & SR_IO) {
  1908.     if (!(foo = NCR5380_pread(instance, d, c - 1))) {
  1909.         /*
  1910.          * We can't disable DMA mode after successfully transfering 
  1911.          * what we plan to be the last byte, since that would open up
  1912.          * a race condition where if the target asserted REQ before 
  1913.          * we got the DMA mode reset, the NCR5380 would have latched
  1914.          * an additional byte into the INPUT DATA register and we'd
  1915.          * have dropped it.
  1916.          * 
  1917.          * The workarround was to transfer one fewer bytes than we 
  1918.          * intended to with the pseudo-DMA read function, wait for 
  1919.          * the chip to latch the last byte, read it, and then disable
  1920.          * pseudo-DMA mode.
  1921.          * 
  1922.          * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
  1923.          * REQ is deasserted when ACK is asserted, and not reasserted
  1924.          * until ACK goes false.  Since the NCR5380 won't lower ACK
  1925.          * until DACK is asserted, which won't happen unless we twiddle
  1926.          * the DMA port or we take the NCR5380 out of DMA mode, we 
  1927.          * can gurantee that we won't handshake another extra 
  1928.          * byte.
  1929.              */
  1930.  
  1931.         while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
  1932.         /* Wait for clean handshake */
  1933.         while (NCR5380_read(STATUS_REG) & SR_REQ);
  1934.         d[c - 1] = NCR5380_read(INPUT_DATA_REG);
  1935.     }
  1936.     } else {
  1937.     int timeout;
  1938.     if (!(foo = NCR5380_pwrite(instance, d, c))) {
  1939.         /*
  1940.          * Wait for the last byte to be sent.  If REQ is being asserted for 
  1941.          * the byte we're interested, we'll ACK it and it will go false.  
  1942.          */
  1943.         if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
  1944.         timeout = 20000;
  1945. #if 1
  1946. #if 1
  1947.         while (!(NCR5380_read(BUS_AND_STATUS_REG) & 
  1948.             BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) &
  1949.                 BASR_PHASE_MATCH));
  1950. #else
  1951.         if (NCR5380_read(STATUS_REG) & SR_REQ) {
  1952.             for (; timeout && 
  1953.             !(NCR5380_read(BUS_AND_STATUS_REG) & BASR_ACK); 
  1954.             --timeout);
  1955.             for (; timeout && (NCR5380_read(STATUS_REG) & SR_REQ);
  1956.             --timeout);
  1957.         } 
  1958. #endif /* 1 */
  1959.     
  1960.  
  1961. #if (NDEBUG & NDEBUG_LAST_BYTE_SENT)
  1962.         if (!timeout) 
  1963.             printk("scsi%d : timed out on last byte\n",
  1964.                 instance->host_no);
  1965. #endif
  1966.  
  1967.  
  1968.         if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
  1969.             hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
  1970.             if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
  1971.             hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
  1972. #if (NDEBUG & NDEBUG_LAST_BYTE_SENT)
  1973.             printk("scsi%d : last bit sent works\n", 
  1974.                 instance->host_no);
  1975. #endif
  1976.             }
  1977.         }
  1978.         } else 
  1979.         while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
  1980. #else /* 1 */
  1981.         udelay (5);
  1982. #endif
  1983.     }
  1984.     }
  1985.  
  1986.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1987.     NCR5380_write(MODE_REG, MR_BASE);
  1988.  
  1989.     *data = d + c;
  1990.     *count = 0;
  1991.     *phase = (NCR5380_read(STATUS_REG & PHASE_MASK));
  1992. #if defined(PSEUDO_DMA) && !defined(UNSAFE)
  1993.     sti();
  1994. #endif /* defined(REAL_DMA_POLL) */
  1995.     return foo;
  1996. #endif /* def REAL_DMA */
  1997. }
  1998. #endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
  1999.  
  2000. /*
  2001.  * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
  2002.  *
  2003.  * Purpose : run through the various SCSI phases and do as the target 
  2004.  *     directs us to.  Operates on the currently connected command, 
  2005.  *    instance->connected.
  2006.  *
  2007.  * Inputs : instance, instance for which we are doing commands
  2008.  *
  2009.  * Side effects : SCSI things happen, the disconnected queue will be 
  2010.  *    modified if a command disconnects, *instance->connected will
  2011.  *    change.
  2012.  *
  2013.  * XXX Note : we need to watch for bus free or a reset condition here 
  2014.  *     to recover from an unexpected bus free condition.
  2015.  */
  2016.  
  2017. static void NCR5380_information_transfer (struct Scsi_Host *instance)
  2018.  
  2019. {
  2020.     NCR5380_local_declare();
  2021.     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) 
  2022.     instance->hostdata;
  2023.     unsigned char msgout = NOP;
  2024.     int len, transfersize;
  2025.     unsigned char *data;
  2026.     unsigned char phase, tmp, old_phase=0xff;
  2027.     Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
  2028.     NCR5380_setup(instance);
  2029.  
  2030.     while (1) {
  2031.     tmp = NCR5380_read(STATUS_REG);
  2032.     /* We only have a valid SCSI phase when REQ is asserted */
  2033.     if (tmp & SR_REQ) {
  2034.         phase = (tmp & PHASE_MASK); 
  2035.         if (phase != old_phase) {
  2036.         old_phase = phase;
  2037. #if (NDEBUG & NDEBUG_INFORMATION)
  2038.         NCR5380_print_phase(instance);
  2039. #endif
  2040.         }
  2041.         switch (phase) {
  2042.         case PHASE_DATAOUT:
  2043. #if (NDEBUG & NDEBUG_NO_DATAOUT)
  2044.         printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n",
  2045.             instance->host_no);
  2046.         msgout = ABORT;
  2047.         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  2048.         break;
  2049. #endif
  2050.         case PHASE_DATAIN:
  2051.         /* 
  2052.          * If there is no room left in the current buffer in the
  2053.          * scatter-gather list, move onto the next one.
  2054.          */
  2055.  
  2056.         if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
  2057.             ++cmd->SCp.buffer;
  2058.             --cmd->SCp.buffers_residual;
  2059.             cmd->SCp.this_residual = cmd->SCp.buffer->length;
  2060.             cmd->SCp.ptr = cmd->SCp.buffer->address;
  2061.             /* ++roman: Try to merge some scatter-buffers if
  2062.              * they are at consecutive physical addresses.
  2063.              */
  2064.             merge_consecutive_buffers( cmd );
  2065. #if (NDEBUG & NDEBUG_INFORMATION)
  2066.             printk("scsi%d : %d bytes and %d buffers left\n",
  2067.             instance->host_no, cmd->SCp.this_residual,
  2068.             cmd->SCp.buffers_residual);
  2069. #endif
  2070.         }
  2071.  
  2072.         /*
  2073.          * The preffered transfer method is going to be 
  2074.          * PSEUDO-DMA for systems that are strictly PIO,
  2075.          * since we can let the hardware do the handshaking.
  2076.          *
  2077.          * For this to work, we need to know the transfersize
  2078.          * ahead of time, since the pseudo-DMA code will sit
  2079.          * in an unconditional loop.
  2080.          */
  2081.  
  2082. /* ++roman: I suggest, this should be
  2083.  *   #if def(REAL_DMA) || def(PSEUDO_DMA) || ...
  2084.  * instead of leaving REAL_DMA out.
  2085.  */
  2086.  
  2087.         CHECK_ICNT("starting transf in inform_transf");
  2088. #if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
  2089. #ifdef NCR5380_dma_xfer_len
  2090.         if (!scsi_devices[cmd->index].borken &&
  2091.             (transfersize = NCR5380_dma_xfer_len(instance, cmd)) > 7) {
  2092. #else
  2093.         if (!scsi_devices[cmd->index].borken && 
  2094.             (transfersize = cmd->transfersize) && 
  2095.             cmd->SCp.this_residual && !(cmd->SCp.this_residual % 
  2096.             transfersize)) {
  2097. #endif
  2098.             len = transfersize;
  2099.             cmd->SCp.phase = phase;
  2100.             if (NCR5380_transfer_dma(instance, &phase,
  2101.             &len, (unsigned char **) &cmd->SCp.ptr)) {
  2102.             /*
  2103.              * If the watchdog timer fires, all future
  2104.              * accesses to this device will use the
  2105.              * polled-IO. */ 
  2106.             printk("scsi%d : switching target %d lun %d to slow handshake\n",
  2107.                 instance->host_no, cmd->target, cmd->lun);
  2108.             scsi_devices[cmd->index].borken = 1;
  2109.             NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
  2110.                 ICR_ASSERT_ATN);
  2111.             msgout = ABORT;
  2112.             } else {
  2113. #ifdef REAL_DMA
  2114.             /* ++roman: When using real DMA,
  2115.              * information_transfer() should return after
  2116.              * starting DMA since it has nothing more to
  2117.              * do.
  2118.              */
  2119.             return;
  2120. #else            
  2121.             cmd->SCp.this_residual -= transfersize - len;
  2122. #endif
  2123.             }
  2124.         } else
  2125. #endif /* defined(REAL_DMA) || defined(REAL_DMA_POLL) */
  2126.           NCR5380_transfer_pio(instance, &phase, 
  2127.             (int *) &cmd->SCp.this_residual, (unsigned char **)
  2128.             &cmd->SCp.ptr);
  2129.         break;
  2130.         case PHASE_MSGIN:
  2131.         /* 
  2132.          * XXX - we don't handle multi-byte messages here, since we 
  2133.          * shouldn't get them after the I_T_L_Q nexus is established
  2134.          * for tagged queuing, and the host should initiate any 
  2135.          * negotiations for sync. SCSI, etc.
  2136.          */
  2137.         len = 1;
  2138.         data = &tmp;
  2139.         NCR5380_transfer_pio(instance, &phase, &len, &data);
  2140.         cmd->SCp.Message = tmp;
  2141.  
  2142.         switch (tmp) {
  2143.         /*
  2144.          * Linking lets us reduce the time required to get the 
  2145.          * next command out to the device, hopefully this will
  2146.          * mean we don't waste another revolution due to the delays
  2147.          * required by ARBITRATION and another SELECTION.
  2148.          *
  2149.          * In the current implementation proposal, low level drivers
  2150.          * merely have to start the next command, pointed to by 
  2151.          * next_link, done() is called as with unlinked commands.
  2152.          */
  2153. #ifdef LINKED
  2154.         case LINKED_CMD_COMPLETE:
  2155.         case LINKED_FLG_CMD_COMPLETE:
  2156. #if (NDEBUG & NDEBUG_LINKED) 
  2157.             printk("scsi%d : target %d lun %d linked command complete.\n",
  2158.             instance->host_no, cmd->target, cmd->lun);
  2159. #endif
  2160.             /* 
  2161.              * Sanity check : A linked command should only terminate with
  2162.              * one of these messages if there are more linked commands
  2163.              * available.
  2164.              */
  2165.  
  2166.             if (!cmd->next_link) {
  2167.              printk("scsi%d : target %d lun %d linked command complete, no next_link\n"
  2168.                 instance->host_no, cmd->target, cmd->lun);
  2169.                 msgout = ABORT;
  2170.                 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
  2171.                 ICR_ASSERT_ATN);
  2172.                 break;
  2173.             }
  2174.  
  2175.             initialize_SCp(cmd->next_link);
  2176.             /* The next command is still part of this process */
  2177.             cmd->next_link->tag = cmd->tag;
  2178.             cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8); 
  2179. #if (NDEBUG & NDEBUG_LINKED) 
  2180.             printk("scsi%d : target %d lun %d linked request done, calling scsi_done().\n",
  2181.             instance->host_no, cmd->target, cmd->lun);
  2182. #endif
  2183.             cmd->scsi_done(cmd);
  2184.             cmd = hostdata->connected;
  2185.             break;
  2186. #endif /* def LINKED */
  2187.         case ABORT:
  2188.         case COMMAND_COMPLETE: 
  2189.             hostdata->connected = NULL;
  2190. #if (NDEBUG & NDEBUG_QUEUES)
  2191.             printk("scsi%d : command for target %d, lun %d completed\n",
  2192.             instance->host_no, cmd->target, cmd->lun);
  2193. #endif
  2194.             hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
  2195.  
  2196.             /* ++roman: For Falcon SCSI, release the lock an on the
  2197.              * ST-DMA here if no other commands are waiting on the
  2198.              * disconnected queue.
  2199.              */
  2200.             falcon_release_lock_if_possible( hostdata );
  2201.             
  2202.             /* 
  2203.              * I'm not sure what the correct thing to do here is : 
  2204.              * 
  2205.              * If the command that just executed is NOT a request 
  2206.              * sense, the obvious thing to do is to set the result
  2207.              * code to the values of the stored parameters.
  2208.              * 
  2209.              * If it was a REQUEST SENSE command, we need some way 
  2210.              * to differentiate between the failure code of the original
  2211.              * and the failure code of the REQUEST sense - the obvious
  2212.              * case is success, where we fall through and leave the result
  2213.              * code unchanged.
  2214.              * 
  2215.              * The non-obvious place is where the REQUEST SENSE failed 
  2216.              */
  2217.  
  2218. #if 0
  2219.             /* ++roman: */
  2220.             if (cmd->SCp.this_residual || cmd->SCp.buffers_residual) {
  2221.             /* We would expect more bytes to transfer, but
  2222.              * the target switch phase ?!?
  2223.              */
  2224.             cmd->result = cmd->SCp.Status |
  2225.                           (cmd->SCp.Message << 8) |
  2226.                       (DID_ERROR << 16);
  2227.             printk( "SCSI: bytes left after transfer: this_residual=%d buffers_residual=%d\n",
  2228.                 cmd->SCp.this_residual,
  2229.                 cmd->SCp.buffers_residual );
  2230.             }
  2231.             
  2232.             else
  2233. #endif
  2234.             if (cmd->cmnd[0] != REQUEST_SENSE) 
  2235.             cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8); 
  2236.             else if (cmd->SCp.Status != GOOD)
  2237.             cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
  2238.             
  2239. #ifdef AUTOSENSE
  2240.             if ((cmd->cmnd[0] != REQUEST_SENSE) && 
  2241.             (cmd->SCp.Status == CHECK_CONDITION)) {
  2242. #if (NDEBUG & NDEBUG_AUTOSENSE) 
  2243.             printk("scsi%d : performing request sense\n", 
  2244.                 instance->host_no);
  2245. #endif
  2246.             cmd->cmnd[0] = REQUEST_SENSE;
  2247.             cmd->cmnd[1] &= 0xe0;
  2248.             cmd->cmnd[2] = 0;
  2249.             cmd->cmnd[3] = 0;
  2250.             cmd->cmnd[4] = sizeof(cmd->sense_buffer);
  2251.             cmd->cmnd[5] = 0;
  2252.  
  2253.             cmd->SCp.buffer = NULL;
  2254.             cmd->SCp.buffers_residual = 0;
  2255.             cmd->SCp.ptr = (char *) cmd->sense_buffer;
  2256.             cmd->SCp.this_residual = sizeof(cmd->sense_buffer);
  2257.  
  2258.             cli();
  2259.             cmd->host_scribble = (unsigned char *) 
  2260.                 hostdata->issue_queue;
  2261.                 hostdata->issue_queue = (Scsi_Cmnd *) cmd;
  2262.                 sti();
  2263. #if (NDEBUG & NDEBUG_QUEUES)
  2264.             printk("scsi%d : REQUEST SENSE added to head of issue queue\n",
  2265.                    instance->host_no);
  2266. #endif
  2267.            } else
  2268. #endif /* def AUTOSENSE */
  2269.             cmd->scsi_done(cmd);
  2270.  
  2271.             NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2272.             
  2273.             while ((NCR5380_read(STATUS_REG) & SR_BSY) && 
  2274.             !hostdata->connected);
  2275.             return;
  2276.         case MESSAGE_REJECT:
  2277.             switch (hostdata->last_message) {
  2278.             case HEAD_OF_QUEUE_TAG:
  2279.             case ORDERED_QUEUE_TAG:
  2280.             case SIMPLE_QUEUE_TAG:
  2281.             scsi_devices[cmd->index].tagged_queue = 0;
  2282.             hostdata->busy[cmd->target] |= (1 << cmd->lun);
  2283.             break;
  2284.             default:
  2285.             break;
  2286.             }
  2287.         case DISCONNECT:
  2288.             scsi_devices[cmd->index].disconnect = 1;
  2289.             cli();
  2290.             cmd->host_scribble = (unsigned char *) 
  2291.             hostdata->disconnected_queue;
  2292.             hostdata->connected = NULL;
  2293.             hostdata->disconnected_queue = cmd;
  2294.             sti();
  2295. #if (NDEBUG & NDEBUG_QUEUES)
  2296.             printk("scsi%d : command for target %d lun %d was moved from connected to"
  2297.                    "  the disconnected_queue\n", instance->host_no, 
  2298.                 cmd->target, cmd->lun);
  2299. #endif
  2300.  
  2301.             /* Enable reselect interupts */
  2302.             NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2303.             /* Wait for bus free to avoid nasty timeouts */
  2304.             while ((NCR5380_read(STATUS_REG) & SR_BSY) && 
  2305.             !hostdata->connected);
  2306.             return;
  2307.         /* 
  2308.          * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
  2309.          * operation, in violation of the SCSI spec so we can safely 
  2310.          * ignore SAVE/RESTORE pointers calls.
  2311.          *
  2312.          * Unfortunately, some disks violate the SCSI spec and 
  2313.          * don't issue the required SAVE_POINTERS message before
  2314.          * disconnecting, and we have to break spec to remain 
  2315.          * compatable.
  2316.          */
  2317.         case SAVE_POINTERS:
  2318.         case RESTORE_POINTERS:
  2319.             break;
  2320.         default:
  2321. /* 
  2322.  * XXX rejected messages should be handled in the pio data transfer phase,
  2323.  * since ATN should be raised before ACK goes false when we reject a message
  2324.  */
  2325.  
  2326.             printk("Unknown message!\n");
  2327.  
  2328. #ifdef notyet
  2329.         /* 
  2330.            * If we get something wierd that we aren't expecting, 
  2331.           * reject it.
  2332.          */
  2333.             msgout = MESSAGE_REJECT;
  2334.             NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
  2335.             ICR_ASSERT_ATN);
  2336. #endif 
  2337.             break;
  2338.         } /* switch (tmp) */
  2339.         break;
  2340.         case PHASE_MSGOUT:
  2341.         len = 1;
  2342.         data = &msgout;
  2343.         hostdata->last_message = msgout;
  2344.         NCR5380_transfer_pio(instance, &phase, &len, &data);
  2345.         if (msgout == ABORT) {
  2346.             hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
  2347.             hostdata->connected = NULL;
  2348.             cmd->result = DID_ERROR << 16;
  2349.             cmd->scsi_done(cmd);
  2350.             NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2351.             falcon_release_lock_if_possible( hostdata );
  2352.             return;
  2353.         }
  2354.         msgout = NOP;
  2355.         break;
  2356.         case PHASE_CMDOUT:
  2357.         len = COMMAND_SIZE(cmd->cmnd[0]);
  2358.         data = cmd->cmnd;
  2359.         /* 
  2360.          * XXX for performance reasons, on machines with a 
  2361.          * PSEUDO-DMA architecture we should probably 
  2362.          * use the dma transfer function.  
  2363.          */
  2364.         NCR5380_transfer_pio(instance, &phase, &len, 
  2365.             &data);
  2366. #ifdef USLEEP
  2367.         if (!disconnect && should_disconnect(cmd->cmnd[0])) {
  2368.             hostdata->time_expires = jiffies + USLEEP_SLEEP;
  2369. #if (NDEBUG & NDEBUG_USLEEP)
  2370.         printk("scsi%d : issued command, sleeping until %ul\n", instance->host_no,
  2371.             hostdata->time_expires);
  2372. #endif
  2373.             NCR5380_set_timer (instance);
  2374.             return;
  2375.         }
  2376. #endif /* def USLEEP */
  2377.         break;
  2378.         case PHASE_STATIN:
  2379.         len = 1;
  2380.         data = &tmp;
  2381.         NCR5380_transfer_pio(instance, &phase, &len, &data);
  2382.         cmd->SCp.Status = tmp;
  2383.         break;
  2384.         default:
  2385.         printk("scsi%d : unknown phase\n", instance->host_no);
  2386. #ifdef NDEBUG
  2387.         NCR5380_print(instance);
  2388. #endif
  2389.         } /* switch(phase) */
  2390.     } /* if (tmp * SR_REQ) */ 
  2391. #ifdef USLEEP
  2392.     else {
  2393.         if (!disconnect && hostdata->time_expires && jiffies > 
  2394.         hostdata->time_expires) {
  2395.         hostdata->time_expires = jiffies + USLEEP_SLEEP;
  2396. #if (NDEBUG & NDEBUG_USLEEP)
  2397.         printk("scsi%d : poll timed out, sleeping until %ul\n", instance->host_no,
  2398.             hostdata->time_expires);
  2399. #endif
  2400.         NCR5380_set_timer (instance);
  2401.         return;
  2402.         }
  2403.     }
  2404. #endif
  2405.     } /* while (1) */
  2406. }
  2407.  
  2408. /*
  2409.  * Function : void NCR5380_reselect (struct Scsi_Host *instance)
  2410.  *
  2411.  * Purpose : does reselection, initializing the instance->connected 
  2412.  *    field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q 
  2413.  *    nexus has been restablished,
  2414.  *    
  2415.  * Inputs : instance - this instance of the NCR5380.
  2416.  *
  2417.  */
  2418.  
  2419.  
  2420. static void NCR5380_reselect (struct Scsi_Host *instance)
  2421.  
  2422. {
  2423.     NCR5380_local_declare();
  2424.     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
  2425.     instance->hostdata;
  2426.     unsigned char target_mask;
  2427.     unsigned char lun, phase;
  2428.     int len;
  2429. #ifdef SCSI2
  2430.     unsigned char tag;
  2431. #endif
  2432.     unsigned char msg[3];
  2433.     unsigned char *data;
  2434.     Scsi_Cmnd *tmp = NULL, *prev;
  2435.     int abort = 0;
  2436.     NCR5380_setup(instance);
  2437.  
  2438.     target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
  2439.  
  2440. #if (NDEBUG & NDEBUG_RESELECTION)
  2441.     printk("scsi%d : reselect\n", instance->host_no);
  2442. #endif
  2443.  
  2444.     /* 
  2445.      * At this point, we have detected that our SCSI ID is on the bus,
  2446.      * SEL is true and BSY was false for at least one bus settle delay
  2447.      * (400 ns).
  2448.      *
  2449.      * We must assert BSY ourselves, until the target drops the SEL
  2450.      * signal.
  2451.      */
  2452.  
  2453.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
  2454.     
  2455.     while (NCR5380_read(STATUS_REG) & SR_SEL);
  2456.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2457.  
  2458.     /*
  2459.      * Wait for target to go into MSGIN.
  2460.      */
  2461.  
  2462.     while (!(NCR5380_read(STATUS_REG) & SR_REQ));
  2463.  
  2464.     len = 3;
  2465.     data = msg;
  2466.     phase = PHASE_MSGIN;
  2467.     NCR5380_transfer_pio(instance, &phase, &len, &data);
  2468.  
  2469. #ifdef SCSI2
  2470.     /*
  2471.      * If there was no residual from the attempt to transfer three bytes, then
  2472.      * the target sent the one byte IDENTIFY message followed by a two byte
  2473.      * queue message.
  2474.      *
  2475.      * If there were two bytes of residual, we got the IDENTIFY message
  2476.      * only.
  2477.      *
  2478.      * If there was one byte of residual, we got the IDENTIFY message
  2479.      * followed by a RESTORE pointers message (which was ignored - 
  2480.      * see MSGIN phase of the NCR5380_information_transfer() function.
  2481.      */
  2482.  
  2483.     if (!len) 
  2484.     tag = msg[2];
  2485.     else
  2486.     tag = 0;
  2487. #endif
  2488.  
  2489.     if (!msg[0] & 0x80) {
  2490.     printk("scsi%d : expecting IDENTIFY message, got ",
  2491.         instance->host_no);
  2492.     print_msg(msg);
  2493.     abort = 1;
  2494.     } else {
  2495.  
  2496.     lun = (msg[0] & 0x07);
  2497.  
  2498.     /* 
  2499.      * Find the command corresponding to the I_T_L or I_T_L_Q  nexus we 
  2500.      * just restablished, and remove it from the disconnected queue.
  2501.      */
  2502.  
  2503.     for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL; 
  2504.         tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble) 
  2505.         if ((target_mask == (1 << tmp->target)) && (lun == tmp->lun)
  2506. #ifdef SCSI2
  2507.         && (tag == tmp->tag) 
  2508. #endif
  2509. ) {
  2510.         if (prev)
  2511.         prev->host_scribble = tmp->host_scribble;
  2512.         else
  2513.         hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble;
  2514.         tmp->host_scribble = NULL;
  2515.         break;
  2516.     }
  2517.  
  2518.     if (!tmp) {
  2519. #ifdef SCSI2
  2520.         printk("scsi%d : warning : target bitmask %02x lun %d tag %d not in disconnect_queue.\n",
  2521.             instance->host_no, target_mask, lun, tag);
  2522. #else
  2523.         printk("scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n",
  2524.             instance->host_no, target_mask, lun);
  2525. #endif
  2526.     /* 
  2527.      * Since we have an established nexus that we can't do anything with,
  2528.      * we must abort it.  
  2529.      */
  2530.     abort = 1;
  2531.     }
  2532.     }
  2533.  
  2534.     if (abort) {
  2535.     msg[0] = ABORT;
  2536.     len = 1;
  2537.     data = msg;
  2538.     phase = PHASE_MSGOUT;
  2539.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  2540.     NCR5380_transfer_pio(instance, &phase, &len, &data);
  2541.     } else {
  2542.     hostdata->connected = tmp;
  2543. #if (NDEBUG & NDEBUG_RESELECTION)
  2544.     printk("scsi%d : nexus established, target = %d, lun = %d, tag = %d\n",
  2545.         instance->host_no, tmp->target, tmp->lun, tmp->tag);
  2546. #endif
  2547.     }
  2548. }
  2549.  
  2550.  
  2551. /*
  2552.  * Function : int NCR5380_abort (Scsi_Cmnd *cmd, int code)
  2553.  *
  2554.  * Purpose : abort a command
  2555.  *
  2556.  * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the 
  2557.  *     host byte of the result field to, if zero DID_ABORTED is 
  2558.  *    used.
  2559.  *
  2560.  * Returns : 0 - success, -1 on failure.
  2561.  *
  2562.  * XXX - there is no way to abort the command that is currently 
  2563.  *      connected, you have to wait for it to complete.  If this is 
  2564.  *     a problem, we could implement longjmp() / setjmp(), setjmp()
  2565.  *      called where the loop started in NCR5380_main().
  2566.  */
  2567.  
  2568. #ifndef NCR5380_abort
  2569. static
  2570. #endif
  2571. int NCR5380_abort (Scsi_Cmnd *cmd, int code)
  2572.  
  2573. {
  2574.     NCR5380_local_declare();
  2575.     struct Scsi_Host *instance = cmd->host;
  2576.     struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
  2577.     instance->hostdata;
  2578.     Scsi_Cmnd *tmp, **prev;
  2579.     unsigned char msg, phase, *msgptr;
  2580.     int len;
  2581.  
  2582.     cli();
  2583.     NCR5380_setup(instance);
  2584.     
  2585. #if (NDEBUG & NDEBUG_ABORT)
  2586.     printk("scsi%d : abort called\n", instance->host_no);
  2587.     printk("        basr 0x%X, sr 0x%X\n", 
  2588.        NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG));
  2589.     NCR5380_print(instance);
  2590.     NCR5380_print_phase(instance);
  2591. #endif
  2592. /* 
  2593.  * Case 1 : If the command hasn't been issued yet, we simply remove it 
  2594.  *         from the issue queue.
  2595.  */
  2596.     for (prev = (Scsi_Cmnd **) &(hostdata->issue_queue), 
  2597.     tmp = (Scsi_Cmnd *) hostdata->issue_queue;
  2598.     tmp; prev = (Scsi_Cmnd **) &(tmp->host_scribble), tmp = 
  2599.     (Scsi_Cmnd *) tmp->host_scribble) 
  2600.     if (cmd == tmp) {
  2601.         (*prev) = (Scsi_Cmnd *) tmp->host_scribble;
  2602.         tmp->host_scribble = NULL;
  2603.         tmp->result = (code ? code : DID_ABORT) << 16;
  2604.         sti();
  2605. #if (NDEBUG & NDEBUG_ABORT)
  2606.     printk("scsi%d : abort removed command from issue queue.\n", 
  2607.     instance->host_no);
  2608. #endif
  2609.         tmp->done(tmp);
  2610.         return 0;
  2611.     }
  2612.  
  2613. /* 
  2614.  * Case 2 : If any commands are connected, we're going to fail the abort
  2615.  *        and let the high level SCSI driver retry at a later time or 
  2616.  *        issue a reset.
  2617.  *
  2618.  *        Timeouts, and therefore aborted commands, will be highly unlikely
  2619.  *          and handling them cleanly in this situation would make the common
  2620.  *        case of noresets less efficient, and would pollute our code.  So,
  2621.  *        we fail.
  2622.  */
  2623.  
  2624.     if (hostdata->connected) {
  2625.     sti();
  2626. #if (NDEBUG & NDEBUG_ABORT)
  2627.     printk("scsi%d : abort failed, command connected.\n", instance->host_no);
  2628. #endif
  2629.         return -1;
  2630.     }
  2631.  
  2632. /*
  2633.  * Case 3: If the command is currently disconnected from the bus, and 
  2634.  *     there are no connected commands, we reconnect the I_T_L or 
  2635.  *    I_T_L_Q nexus associated with it, go into message out, and send 
  2636.  *      an abort message.
  2637.  *
  2638.  * This case is especially ugly. In order to resetablish the nexus, we
  2639.  * need to call NCR5380_select().  The easiest way to implement this 
  2640.  * function was to abort if the bus was busy, and let the interrupt
  2641.  * handler triggered on the SEL for reselect take care of lost arbitrations
  2642.  * where necessary, meaning interrupts need to be enabled.
  2643.  *
  2644.  * When interrupts are enabled, the queues may change - so we 
  2645.  * can't remove it from the disconnected queue before selecting it
  2646.  * because that could cause a failure in hashing the nexus if that 
  2647.  * device reselected.
  2648.  * 
  2649.  * Since the queues may change, we can't use the pointers from when we
  2650.  * first locate it.
  2651.  *
  2652.  * So, we must first locate the command, and if NCR5380_select()
  2653.  * succeeds, then issue the abort, relocate the command and remove
  2654.  * it from the disconnected queue.
  2655.  */
  2656.  
  2657.     for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp; 
  2658.     tmp = (Scsi_Cmnd *) tmp->host_scribble) 
  2659.         if (cmd == tmp) {
  2660.             sti(); 
  2661. #if (NDEBUG & NDEBUG_ABORT)
  2662.     printk("scsi%d : aborting disconnected command.\n", instance->host_no);
  2663. #endif
  2664.   
  2665.             if (NCR5380_select (instance, cmd, (int) cmd->tag)) 
  2666.         return 1;
  2667.  
  2668. #if (NDEBUG & NDEBUG_ABORT)
  2669.     printk("scsi%d : nexus restablished.\n", instance->host_no);
  2670. #endif
  2671.  
  2672.         msg = ABORT;
  2673.         msgptr = &msg;
  2674.         len = 1;
  2675.         phase = PHASE_MSGOUT;
  2676.         NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  2677.         NCR5380_transfer_pio (instance, &phase, &len, &msgptr);
  2678.  
  2679.         cli();
  2680.         for (prev = (Scsi_Cmnd **) &(hostdata->disconnected_queue), 
  2681.         tmp = (Scsi_Cmnd *) hostdata->disconnected_queue;
  2682.         tmp; prev = (Scsi_Cmnd **) &(tmp->host_scribble), tmp = 
  2683.         (Scsi_Cmnd *) tmp->host_scribble) 
  2684.             if (cmd == tmp) {
  2685.             *prev = (Scsi_Cmnd *) tmp->host_scribble;
  2686.             tmp->host_scribble = NULL;
  2687.             tmp->result = (code ? code : DID_ABORT) << 16;
  2688.             sti();
  2689.             tmp->done(tmp);
  2690.             return 0;
  2691.         }
  2692.     }
  2693.  
  2694. /*
  2695.  * Case 4 : If we reached this point, the command was not found in any of 
  2696.  *        the queues.
  2697.  *
  2698.  * We probably reached this point because of an unlikely race condition
  2699.  * between the command completing successfully and the abortion code,
  2700.  * so we won't panic, but we will notify the user in case somethign really
  2701.  * broke.
  2702.  */
  2703.  
  2704.     sti();
  2705.     printk("scsi%d : warning : SCSI command probably completed successfully\n"
  2706.            "         before abortion\n", instance->host_no); 
  2707.     return 0;
  2708. }
  2709.  
  2710.  
  2711. /* 
  2712.  * Function : int NCR5380_reset (Scsi_Cmnd *cmd)
  2713.  * 
  2714.  * Purpose : reset the SCSI bus.
  2715.  *
  2716.  * Returns : 0
  2717.  *
  2718.  */ 
  2719.  
  2720. #ifndef NCR5380_reset
  2721. static
  2722. #endif
  2723. int NCR5380_reset( Scsi_Cmnd *cmd )
  2724.  
  2725. {   unsigned long timeout;
  2726.     unsigned      oldflags;
  2727.     int           i;
  2728.     struct NCR5380_hostdata *hostdata =
  2729.     (struct NCR5380_hostdata *)cmd->host->hostdata;
  2730.     NCR5380_local_declare();
  2731.     NCR5380_setup(cmd->host);
  2732.  
  2733. #if (NDEBUG & NDEBUG_ABORT)
  2734.     printk( "scsi%d: doing SCSI reset!\n", cmd->host->host_no );
  2735. #endif
  2736.     
  2737.     /* get in phase */
  2738.     NCR5380_write( TARGET_COMMAND_REG,
  2739.            PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
  2740.     /* assert RST */
  2741.     NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
  2742.  
  2743.     /* Wait for 40 .. 50 ms (min. is 25 ms) */
  2744.     save_flags( oldflags );
  2745.     sti();
  2746.     for( timeout = jiffies + 5; jiffies < timeout; )
  2747.     ;
  2748.     restore_flags( oldflags );
  2749.     
  2750.     /* reset NCR registers */
  2751.     NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
  2752.     NCR5380_write( MODE_REG, MR_BASE );
  2753.     NCR5380_write( TARGET_COMMAND_REG, 0 );
  2754.     NCR5380_write( SELECT_ENABLE_REG, 0 );
  2755.  
  2756.     /* After the reset, there are no more connected or disconnected commands
  2757.      * and no busy units */
  2758.     cli();
  2759.     if ((cmd = (Scsi_Cmnd *)hostdata->connected)) {
  2760. #if (NDEBUG & NDEBUG_ABORT)
  2761.     printk( "scsi%d: reset aborted a connected command\n", cmd->host->host_no);
  2762. #endif
  2763.     cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
  2764.     cmd->scsi_done( cmd );
  2765.     hostdata->connected = NULL;
  2766.     }
  2767.  
  2768.     for( i = 0, cmd = (Scsi_Cmnd *)hostdata->disconnected_queue; cmd;
  2769.      cmd = (Scsi_Cmnd *)cmd->host_scribble, ++i ) {
  2770.     cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
  2771.     cmd->scsi_done( cmd );
  2772.     }
  2773. #if (NDEBUG & NDEBUG_ABORT)
  2774.     if (i > 0)
  2775.     printk( "scsi%d: reset aborted %d disconnected command(s)\n",
  2776.         cmd->host->host_no, i );
  2777. #endif
  2778.     hostdata->disconnected_queue = NULL;
  2779.     sti();
  2780.     
  2781.     for( i = 0; i < 8; ++i )
  2782.     hostdata->busy[i] = 0;
  2783.  
  2784. #if defined(REAL_DMA) || defined(REAL_DMA_POLL)
  2785.     hostdata->dma_len = 0;
  2786. #endif
  2787.     
  2788.     return 0;
  2789. }
  2790.  
  2791.